Hibernate OnetoMany, ManyToOne Mapping Gives null

I have 2 classes called PurchaseList.java and PurchaseListItems.java

I need to map PurchaseList to PurchaseListItems

PurchaseList.java

@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name="pl_id",referencedColumnName="id") private List<PurchaseListItems> purchaseListItems; 

PurchaseListItems.java

 @ManyToOne @JoinColumn(name="pl_id") private PurchaseList purchaseListId; 

Everything is fine, but I get null in pl_id. Please let me know where I am wrong.

+8
java hibernate jpa
source share
7 answers

Your mapping actually defines two independent unidirectional relationships. What you want is one bi-directional relationship. The following code will establish a bi-directional relation

 @OneToMany(cascade = CascadeType.ALL, mappedBy = "purchaseListId") @JoinColumn(name="pl_id",referencedColumnName="id") private List<PurchaseListItems> purchaseListItems; 

The mappedBy attribute is required because the provider cannot automatically determine that the specified relationship actually forms a single relationship. You can use the Java type of an instance member, but then if you have multiple members of the same type. And there are many scenarios where you have two separate relationships. Example:

OneToMany: User -> ForumThread (threads created by the user)

ManyToOne: ForumThread -> User (the user who closed the thread is obviously not necessarily the one who started the thread)

These are two independent relationships and should be considered as such. You would be surprised if your perseverance provided only a bi-directional connection from this only because the types and plurality were consistent.

Also note that bidirectional relationships are not automatically managed by any JPA provider, which means that the reverse side is not automatically updated / installed in your object model and therefore is not in db. You have to do it yourself. By the way, in all my projects, bidirectional relationships were a pain in the ass, and I think it is advisable to avoid them.

+5
source share

For some reason, the picture does not work for me with postgres sql and Hibernate4

Below image displayed

PurchaseList.java

 @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name="pl_id",nullable=false) private List<PurchaseListItems> purchaseListItems; 

PurchaseListItems.java

 @ManyToOne @JoinColumn(name="pl_id", nullable=false,insertable=false,updatable=false ) private PurchaseList purchaseListId; 

Note: you must use Identity or Explicitly mention Sequence columns for id for postgres.

 @GeneratedValue(strategy=GenerationType.IDENTITY) 
+1
source share

The jpa specification looks good, but make sure you give a valid parent relationship with the children in the database. If the link is not specified, it will return null.

0
source share

try it

 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchaseListId") 
0
source share

Check if the buyListId was filled with a valid value (created instance of PurchaseList ) when creating the PurchaseListItems value.

It is better to use mappedBy as shown below to allow many parties to maintain a relationship.

 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchaseListId") @JoinColumn(name="pl_id",referencedColumnName="id") private List<PurchaseListItems> purchaseListItems; 
0
source share
 for(PurchaseListItems item:purchaseListItemsList) item.purchaseListId(PurchaseList); 

This is what I missed when I create the object.

Thnaks for your answers

0
source share
  • Annotation @JoinColumn belongs to the @ManyToOne side of the relationship, but not to the @OneToMany side - delete it from @OneToMany.

  • The cascade is used to cascade DELETE / READ / UPDATE ... operations, but it does not automatically populate the identifier column on the "child" side of the foreign key. In fact, it does not populate java links to objects on both sides of the FK relationship. You need to manually configure the relationship data on both sides of the bidirectional relationship:

    myPurchaseListItem.setPurchaseList (myPurchaseList);
    myPurchaseList.setPurchaseListItem (myPurchaseListItem);

    From the JPA 2 specification:

    Bidirectional relationships between managed entities will be maintained based on links owned by the owner of the relationship. It is the responsibility of the developers to keep in memory the links stored on the owner’s side, and those on the reverse side are consistent with each other when they change. In the case of one-to-one and one-to-many relationships, the responsibility of developers is to ensure (sic) that the semantics of the relationships are respected. [29]

    It is especially important to ensure that changes in the back of the relationship result in appropriate updates on the owner's side to ensure that the changes are not lost when they are synchronized with the database.

0
source share

All Articles