-->
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.  [ 4 posts ] 
Author Message
 Post subject: one-to-one -- trying to do cascading insert and lazy load
PostPosted: Mon Oct 04, 2004 3:50 pm 
Newbie

Joined: Mon Oct 04, 2004 2:45 pm
Posts: 2
Location: Boulder, Colorado, USA
It appears that there is no way to get cascading insert and lazy loading to work using the same one-to-one mapping definition.

In the docs/code below, I am attempting to use constrained="true" so that lazy load will work. But when I use this then my cascading insert fails. Hibernate seems to be trying to insert the child (User) before the parent (Person), and it has a null id.

If I switch to use constrained="false" then the cascading insert works, but then the load is always "eager."

We have tried every permiatation we can think of, but with no success. The only example that is close to detailed I read here: http://forum.hibernate.org/viewtopic.ph ... toone+lazy. However, from what I can see the child's id is "assigned" rather than "foreign". I believe this implies that my parent (Person) would have to be saved before the child (User), and then the parent's id would be assigned to the child manually by the app.
That is not a desirable solution for us.

HELP!


Hibernate version: 2.1

Mapping documents:

Code:
<hibernate-mapping package="mappings.one_to_one">

  <class name="Person" table="PERSON">
   
    <id name="oid" column="PERSON_SEQ_NO">
      <generator class="native"/>
    </id>
      
    <property name="firstName" column="FIRST_NAME"/>
    <property name="lastName" column="LAST_NAME"/>
    <property name="middleName" column="MIDDLE_NAME"/>

    <one-to-one name="user" class="User" constrained="true" outer-join="false"/>
      
  </class>

</hibernate-mapping>

<hibernate-mapping package="mappings.one_to_one">

  <class name="User" table="USER" proxy="User">
   
    <id name="oid" column="PERSON_SEQ_NO">
      <generator class="foreign">
        <param name="property">person</param>
      </generator>
    </id>
      
    <property name="password" column="PASSWORD"/>
    <property name="username" column="USERNAME"/>

    <one-to-one name="person" class="Person" constrained="true" />
  </class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
    // inside a JUnit test . . .

    PersonIF tempPerson = new Person();
    tempPerson.setFirstName("Vaughn");
    tempPerson.setLastName("Vernon");
    tempPerson.setMiddleName("L");
   
    UserIF tempUser = new User();
    tempUser.setPassword("pw");
    tempUser.setUsername("vvernon");
   
    tempPerson.setUser(tempUser);
    tempUser.setPerson(tempPerson);
   
    Session tempSession = this.getSession();
   
    // core create test
    Transaction tempTx = tempSession.beginTransaction();
    tempSession.save(tempPerson);
    tempTx.commit();
    tempSession.close();
   
    // follow up with read test
    tempSession = this.getSession(); // make sure not cached instance
    Long tempOid = ((Person) tempPerson).getOid();
    tempTx = tempSession.beginTransaction();
    tempPerson = (PersonIF) tempSession.get(Person.class, tempOid);
    tempTx.commit();


Full stack trace of any exception that occurs:

Code:
1) testCreateOneToOne(mappings.one_to_one.TestOneToOneMapping)net.sf.hibernate.id.IdentifierGenerationException: null identifier generated
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:762)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1380)
   at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:883)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:850)
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:768)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)
   at mappings.one_to_one.TestOneToOneMapping.testCreateOneToOne(TestOneToOneMapping.java:69)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at mappings.one_to_one.TestOneToOneMapping.main(TestOneToOneMapping.java:21)


Name and version of the database you are using: mysql-4.0.20d-win

The generated SQL (show_sql=true):

Code:
Hibernate: select person0_.PERSON_SEQ_NO as PERSON_S1_, person0_.FIRST_NAME as FIRST_NAME, person0_.LAST_NAME as LAST_NAME, person0_.MIDDLE_NAME as MIDDLE_N4_ from PERSON person0_


Debug level Hibernate log excerpt:

_________________
Thanks!

Vaughn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 7:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Did you looked at that
http://www.hibernate.org/162.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 7:50 pm 
Newbie

Joined: Mon Oct 04, 2004 2:45 pm
Posts: 2
Location: Boulder, Colorado, USA
emmanuel wrote:
Did you looked at that
http://www.hibernate.org/162.html


Yes I did. The trouble I am having is that I can never find both sides of the example. The examples generally focus on the parent class side of the problem.

Like I stated above, it appears from the examples I can find, that you have to assign the child id rather than have it generated from the cascading insert. It appears that the cascading insert and lazy load are mutually exclusive features in one-to-one.

Thanks for your reply.

Vaughn


Top
 Profile  
 
 Post subject: Cascading Insert
PostPosted: Wed Oct 13, 2004 10:33 am 
Beginner
Beginner

Joined: Thu May 27, 2004 3:07 pm
Posts: 20
I cannot even get my cascading insert to work with hibernate 2.1. For some reason, no matter what i do, inserting the parent object (MasterField) only causes an insert to the MASTER_FIELD table, and the DATA_TYPE_PROPERTIES table. I've tried screwing around with the constrained flags, but to no avail.

Mapping document:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="com.synygy.product.data.masterfield.MasterFieldImpl"
proxy="com.synygy.product.data.masterfield.MasterField"
table="MASTER_FIELD"
lazy="false">

<id name="id" column="ID" type="integer" unsaved-value="0">
<generator class="native"/>
</id>

<property name="name" column="NAME" not-null="true"/>

<one-to-one name="properties"
class="com.synygy.product.data.masterfield.AbstractDataTypeProperties"
cascade="save-update"
constrained="false" />
</class>

<class name="com.synygy.product.data.masterfield.AbstractDataTypeProperties"
proxy="com.synygy.product.data.masterfield.DataTypeProperties"
table="DATA_TYPE_PROPERTIES"
lazy="false">

<id name="id" column="ID">
<generator class="foreign">
<param name="property">masterField</param>
</generator>
</id>

<discriminator column="DATA_TYPE" type="string"/>

<one-to-one name="masterField"
class="com.synygy.product.data.masterfield.MasterFieldImpl"
constrained="true" />

<subclass name="com.synygy.product.data.masterfield.BooleanPropertiesImpl"
discriminator-value="1"
proxy="com.synygy.product.data.masterfield.BooleanProperties"
lazy="false">

</subclass>

<subclass name="com.synygy.product.data.masterfield.CharacterPropertiesImpl"
discriminator-value="2"
proxy="com.synygy.product.data.masterfield.CharacterProperties"
lazy="false">
<property name="length" column="LENGTH" not-null="true"/>
</subclass>

</class>

</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.