简体   繁体   中英

Hibernate (Spring JPA): @ManyToOne JoinColumns (which are an EmbeddedId) are null

I didn't see my error and after a research on stackoverflow and Google I think the code should be correct. But Hibernate (spring-boot-starter-data-jpa 2.2.4) still fill the JoinColumns with null .

Here is my OneToMany class:

@Entity
@Table(name = "tablea", schema = "")
public class TableAEntity implements Serializable {

    private static final long serialVersionUID = 7890327260188587351L;

    @EmbeddedId
    private MyId id;
    
    @OneToMany(cascade = ALL, mappedBy = "tableA", orphanRemoval = true, fetch = FetchType.LAZY)
    private List<TableBEntity> tableBentries;
    
    // Getter + Setter
}

My EmbeddedId class:

@Embeddable
public class MyId implements Serializable {

    private static final long serialVersionUID = -8267953052238233498L;

    @Column(name = "id")
    private String id;

    @Column(name = "iddate")
    private Date iddate;

    @Column(name = "idint")
    private BigInteger idint;
    
    // Getter + Setter
}

And finally my ManyToOne class:

@Entity
@Table(name = "tableB", schema = "")
public class TableBEntity implements Serializable {

    private static final long serialVersionUID = -4648090658471459969L;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
        @JoinColumn(name = "id", referencedColumnName = "id"),
        @JoinColumn(name = "iddate", referencedColumnName = "iddate"),
        @JoinColumn(name = "idint", referencedColumnName = "idint")
    })
    private TableAEntity tableA;
    
    // Some other attributes
    
    // Getter + Setter
}

As you may see I want to use the attributes ( id , iddate and idint ) as a combined primaray key in both tables.

When I create the TableAEntity object I add several TableBEntity objects to the tableBentries attribute. And for every TableBEntity I set the reference to the TableAEntity (attribute tableA ). And of course the MyId (attribute id ) object is filled.

After saveing the TableAEntity object, Hibernate also stores all TableBEntity but the fields id , iddate and idint (all JoinColumn's) are null .

Any idea?

It seems that @Id in my TableBEntity causes the problems. If I moved it to another attribute, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM