-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Many-to-one associated entities save problem
PostPosted: Sat Jan 13, 2007 2:13 pm 
Newbie

Joined: Sun Dec 17, 2006 10:15 am
Posts: 6
Location: Prague
Hi, please help with the following issue.

I have two entities:

Code:
@Entity
@Table(name="SHOPPING_SESSIONS")
public class ShoppingSession {
  ...
  @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY)
  @JoinColumn(name="WEB_SESSION_ID", nullable=false)
  private WebSession webSession;
  ...
}

@Entity
@Table(name="WEB_SESSIONS")
public class WebSession {
  @Id
  @Column(name="WEB_SESSION_ID", updatable=false)
  private String id;
  ...
  @OneToMany(mappedBy="webSession" cascade=CascadeType.REFRESH, fetch=FetchType.LAZY)
  @JoinColumn(name="WEB_SESSION_ID")
  private Set<ShoppingSession> shoppingSessions;
  ...
}


Now, this is typical code performed when new shopping cart is created in eshop (unlimited shopping session may be created in one web session):
Code:
ShoppingSession shoppingSession = new ShoppingSession();
if (webSession == null) {
  webSession = new WebSession();
  webSession.setId(request.getSession().getId());
}
shoppingSession.setWebSession(webSession);

When shopping cart is being saved the persist() method is called for save ShoppingSession to the DB. Hibernate calls INSERT on both SHOPPING_SESSIONS and WEB_SESSIONS tables.

Now, the new shopping cart (ShoppingSession entity) is created in the same web session and the first above code is perfomed again. In this time the once saved WebSession is set to the newlly created ShoppingSession. When I try to save the new ShoppingSession, Hibernate calls INSERT on WEB_SESSIONS again. This call fails because unique WEB_SESSION_ID constraint.

Please help me with this situation and advice correct solution.

Thank you very much.

_________________
Tomáš Klíma, Aiteq Ltd.
tomas.klima@aiteq.com | skype: tomas.klima | icq: 117-871-950 | www.aiteq.cz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 4:03 am 
Newbie

Joined: Mon Jan 15, 2007 3:53 am
Posts: 2
I am facing the same problem. This can be done in the following way



Code:
ShoppingSession shoppingSession = new ShoppingSession();
if (webSession == null) {
  // If WebSession with id already exists get that
  // Else create a new one.
}
shoppingSession.setWebSession(webSession);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 7:55 am 
Newbie

Joined: Sun Dec 17, 2006 10:15 am
Posts: 6
Location: Prague
Thank you. A similar solution I used too. But now, I know a matter of this issue. It is very important thing when using Hibernate with Spring framework.

In Hibernate, the above situation becames when the entity changes state to detached between the both persist() callings. I couldn't enter why this happens in my application. Now I know why thanks to my friend Milan Cecrdle. In my Spring web application I'am using the OpenSessionInViewFilter filter. It ensures that all persistent entities changes their state to detached after rendering a view.
Milan, thank you for explanation.

_________________
Tomáš Klíma, Aiteq Ltd.
tomas.klima@aiteq.com | skype: tomas.klima | icq: 117-871-950 | www.aiteq.cz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 6:52 am 
Newbie

Joined: Mon Jan 15, 2007 3:53 am
Posts: 2
Can u please tell me the solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 8:36 am 
Newbie

Joined: Sun Dec 17, 2006 10:15 am
Posts: 6
Location: Prague
Solution 1
You can break association in domain objects and store only webSessionId (instead reference to webSession object) in the ShoppingSession. Then you must manage WebSession and ShoppingSession separately.

Solution 2
Before saving new shoppingSession (with reference to already stored webSession) you must call the merge() on webSession object what changes its state to persistent.

_________________
Tomáš Klíma, Aiteq Ltd.
tomas.klima@aiteq.com | skype: tomas.klima | icq: 117-871-950 | www.aiteq.cz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.