P
P
Pavel Dudenkov2016-07-25 12:33:02
Java
Pavel Dudenkov, 2016-07-25 12:33:02

Hibernate feedback - null?

Hello, I have been struggling with a problem for a long time, about which they write a lot, it googles well, but in my case it is not solved for some reason.

@Entity
@Table(name = EVENT_CLASS_FAMILY_TABLE_NAME)
public class EventClassFamily extends GenericModel<EventClassFamilyDto> {

    private static final long serialVersionUID = 3766947955702551264L;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = EVENT_CLASS_FAMILY_VERSION_EVENT_CLASS_FAMILY_ID, nullable = false)
    private List<EventClassFamilyVersion> schemas;

@Entity
@Table(name = EVENT_CLASS_FAMILY_VERSION_TABLE_NAME)
public class EventClassFamilyVersion extends GenericModel<EventClassFamilyVersionDto> {

    private static final long serialVersionUID = -7490111487256831990L;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ecfv")
    //@OneToMany(cascade = CascadeType.ALL)
    //@JoinColumn(name = EVENT_CLASS_EVENT_CLASS_FAMILY_VERSION_ID, nullable = false)
    private List<EventClass> records;

@Entity
@Table(name = EVENT_CLASS_TABLE_NAME)
public class EventClass extends GenericModel<EventClassDto> {

    private static final long serialVersionUID = 3766947955702551264L;

    @ManyToOne
    @JoinColumn(name = EVENT_CLASS_TENANT_ID, nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Tenant tenant;

    @ManyToOne(cascade = CascadeType.ALL)
//    @OnDelete(action = OnDeleteAction.CASCADE)
    private EventClassFamilyVersion ecfv;

At the time of saving the EventClassFamily I have: an EventClassFamily with an already generated Id , an EventClassFamilyVersion with Id=null since it hasn't been saved yet, and a List , each with Id=null .
After saving, everything is fine, except that in all EventClass feedback on EventClassFamilyVersion (ecfv) = null , although the annotations seem to indicate that there is a two-way relationship.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2016-07-26
@viperz

To get sets of related objects (@OneToMany and @ManyToMany) you need to specify fetch=FetchType.EAGER in the link annotation. This is convenient, but there is a pitfall - the number of queries to the database will increase, just like the amount of data transferred.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question