-->
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: one-to-one saveOrUpdate problem
PostPosted: Thu Sep 11, 2003 4:32 am 
Newbie

Joined: Thu Sep 11, 2003 3:47 am
Posts: 2
Hello,
My setup is JBoss 3.2.1, with Hibernate 2.03 installed as an MBean running against MS SQLServer 8. All Hibernate calls are actioned through Session EJBs with CMT, the Hibernate session being closed at the end of each EJB method invocation.

We've got a one-to-one relationship set up as follows:


Code:
default-cascade="none"

<class name="com.psi.User" table="UserTable">
   <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="native"/>
   </id>
   <property name="name" type="string" unique="true"/>
   <property name="password" type="string"/>
   <joined-subclass name="com.psi.Customer" table="Customer">
      <key column="id"/>
      <many-to-one name="company"
         column="company"
         class="com.psi.Company"/>
      <one-to-one name="Account" class="com.psi.Account" />
   </joined-subclass>
</class>

<class name="com.psi.Account" table="Account">
   <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="foreign">
         <param name="property">Customer</param>
      </generator>
   </id>
   <property name="name" type="string"/>
   <one-to-one name="Customer" class="com.psi.Customer" />
</class>


What we're attempting to do is add new (transient) Account to an already existing Customer, and then save it via a session bean call. What actually happens is that Hibernate attempts to create a new instance of Customer on the database, which fails because of the unique constraint on Customer.name. If the constraint is remove we get multiple duplicate rows.

Our current none-working code looks like this:

Code:
{
   ...
   customer.setAccount(account);
   account.setCustomer(customer);
   ...
}

{
   //ejb method
   ...
   session.saveOrUpdate(account);
   session.flush;
   session.close;
   ...
}


I've tried various combinations of composition and bean saving, but mthing has made any difference. Can anyone shed any light on this, please?

Will


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 6:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
sounds like the typical kind of thing that happens due to a bad unsaved-value mapping


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 7:38 am 
Newbie

Joined: Thu Sep 11, 2003 3:47 am
Posts: 2
Gavin,

thanks for your reply. Can you point me in the direction of a good unsaved-value mapping?

Will


Top
 Profile  
 
 Post subject: not a bad unsaved-value, but a bug in one-to-one handling
PostPosted: Wed Sep 24, 2003 10:32 pm 
Newbie

Joined: Thu Sep 11, 2003 10:16 pm
Posts: 4
This is not a bad unsaved-value problem.
I use the XY.hbm.xml from net.sf.hibernate.test where the unsaved-value for the id is set to "0" explicitly. I got the same problem.


Using the XY.hbm.xml in the net.sf.hibernate.test package as example,
when I associate a X object for a Y object loaded in previous closed session, and save the X, the new session can not detect that the Y is already an object from database. It always generates a new Y object by inserting a row into the database.
I think this is a bug. If it is a feature, what is the consideration after
this feature?

Thanks.


I have the following code:
----------------------coding-----------------
SessionFactory sf= config.buildSessionFactory();
Session session= sf.openSession();

//---- loading a object and close the session.
Y y=(Y)session.load(Y.class, new Long(1));
session.close();

// associate a new X object for the loaded Y.
X x=new X();
x.setY(y);
y.setTheX(x);

//----- open a new session.
session=sf.openSession();
Transaction tx=session.beginTransaction();
// critical line.
/*
If I call the saveOrUpdate(y), the session correctly detected
that a new X object is created, and insert a row into X
If I call the saveOrUpdate(x), the session always first create a
new Y object (by inserting a row in Y) and insert a row into X.

In my mind, the session should detect that the Y object is a
persistent object from previouse session and it should not generate a new Y object.

Is this a bug or a feature?
If it is a feature, what is the consideration for this feature?
*/
session.saveOrUpdate(y);
tx.commit();
session.close();
---------------------------code-------------------

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 10:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
OK, its true that ForeignGenerator does not handle detached objects correctly. I'll see about fixing this.


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.