Hibernate Map ID automatically from field

后端 未结 1 1221
慢半拍i
慢半拍i 2021-01-27 10:30

I have something similar to this:

@Entity
@Table(name = \"claim\", schema = \"test\")
public class Claim implements Serializable {

   @Id
   @GeneratedValue(str         


        
相关标签:
1条回答
  • 2021-01-27 11:00

    I have tried using the following mappings and from your comments it was apparent that Json object is populated correctly. I have noticed that the annotation @MapsId is the culprit.If you check the documentation of @MapsId annotation it says

    Blockquote

    The name of the attribute within the composite key * to which the relationship attribute corresponds. If not * supplied, the relationship maps the entity's primary * key

    Blockquote

    If you change @MapsId("Claim_idClaim") to @MapsId it will start persisting your entities.

    import javax.persistence.*;
    
    @Entity
    @Table(name = "CLAIM")
    public class Claim {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "idClaim", unique = true, nullable = false)
        private Long idClaim;
        @Column(name = "notes")
        private String notes;
    
        @OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, optional = false)
        private ClaimReturnInfo claimReturnInfo;
    
        public Long getIdClaim() {
            return idClaim;
        }
    
        public String getNotes() {
            return notes;
        }
    
        public void setNotes(String notes) {
            this.notes = notes;
        }
    
    
        public ClaimReturnInfo getClaimReturnInfo() {
            return claimReturnInfo;
        }
    
        public void setClaimReturnInfo(ClaimReturnInfo claimReturnInfo) {
            if (claimReturnInfo == null) {
                if (this.claimReturnInfo != null) {
                    this.claimReturnInfo.setClaim(null);
                }
            } else {
                claimReturnInfo.setClaim(this);
            }
            this.claimReturnInfo = claimReturnInfo;
        }
    
    }
    
    
    package com.hiber.hiberelations;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "CLAIM_RETURN_INFO")
    public class ClaimReturnInfo {
    
        @Id
        @Column(name = "Claim_idClaim")
        private Long childId;
    
    
        @Column(name = "DESCRIPTION")
        private String description;
    
        @MapsId
        @OneToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "Claim_idClaim")
        private Claim claim;
    
        public Long getChildId() {
            return childId;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Claim getClaim() {
            return this.claim;
        }
    
        public void setClaim(Claim claim) {
            this.claim = claim;
        }
    }
    
    0 讨论(0)
提交回复
热议问题