-->
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.  [ 9 posts ] 
Author Message
 Post subject: simple one-to-one mapping issue
PostPosted: Wed Aug 11, 2004 2:18 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
This is actually quite embaressing. I am simply trying to do a one-to-one relationship between an Order and a PaymentInformation object. The Primary Key ORDERID from Order is the Primary Key and Foreign Key on PaymentInformation. But the information is getting entered in with a value of 0 for ORDERID in the PaymentInformation object.

But I can't for the life of me figure out what I am doing wrong. I am sure it is a very simple problem. Any help is greatly appreciated.

Hibernate version: 2.1.4

Mapping documents:
Code:
<hibernate-mapping>
  <class name="com.gpani.commerce.paytrust.beans.order.Order" table="ORDERS" dynamic-update="false" dynamic-insert="false">
    <id name="orderId" column="ORDERID" type="java.lang.Long" unsaved-value="null">
      <generator class="native"></generator>
    </id>

    <one-to-one name="paymentInformation" class="com.gpani.commerce.paytrust.beans.order.PaymentInformation" cascade="all" outer-join="false" constrained="true"/>
  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="com.gpani.commerce.paytrust.beans.order.PaymentInformation" table="PAYMENTINFORMATIONS" dynamic-update="false" dynamic-insert="false">
    <id name="orderId" column="ORDERID" type="java.lang.Long" unsaved-value="null">
      <generator class="native"></generator>
    </id>

    <property name="cardHolderName" type="java.lang.String" update="false" insert="true" column="CARDHOLDERNAME" not-null="true"/>
  </class>
</hibernate-mapping>



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

The following section of code is within a try-catch loop
Code:
  hibernateSession = HibernateManager.currentSession();
  transaction = hibernateSession.beginTransaction();
  hibernateSession.saveOrUpdate(Order);
  transaction.commit();



Name and version of the database you are using: MySQL 4.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:25 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
You specified generator=native, this uses either 'identity' or 'sequence' depending on the database. Which databse are you using? IIRC, if SQLServer or MySQL, you have to identify the column as an Identity column.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:40 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
dplass wrote:
You specified generator=native, this uses either 'identity' or 'sequence' depending on the database. Which databse are you using? IIRC, if SQLServer or MySQL, you have to identify the column as an Identity column.



Thanks for your feedback. I am using MySQL 4.1.

If I change the mapping on the PaymentInformation object to use identity instead of native (which I understand is wrong), I get the following error.

java.sql.SQLException: Duplicate key or integrity constraint violation, message from server: "Duplicate entry '0' for key 1

I guess the problem in my case is that when the Order object is saved, the generated orderId value is not passed into the PaymentInformation object. Is there some sort of an inverse relationship that needs to be maintained between the two objects that I am not doing?

TIA


Changed mapping that threw the new exception.


Code:
<hibernate-mapping>
  <class name="com.gpani.commerce.paytrust.beans.order.PaymentInformation" table="PAYMENTINFORMATIONS" dynamic-update="false" dynamic-insert="false">
    <id name="orderId" column="ORDERID" type="java.lang.Long" unsaved-value="null">
      <generator class="identity"></generator>
    </id>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:48 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
I never used one-to-one, but don't you also have to have the corresponding one-to-one on the child object to the parent object?

Also, dumb question, you truncated your table before running, right? (I.e. record 0 really wasn't there to start with, right?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 3:29 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
dplass wrote:
I never used one-to-one, but don't you also have to have the corresponding one-to-one on the child object to the parent object?

Also, dumb question, you truncated your table before running, right? (I.e. record 0 really wasn't there to start with, right?)



Yes, I did truncate all the data. So there were no records in the database for the tables. I am unsure about the one-to-one being on both the parent and child objects. Does anyone else have an answer for this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 5:58 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Well, I guess I have taken a tumble off the stupid tree today (as opposed to other days when I just jump out of it head first).

http://www.hibernate.org/hib_docs/refer ... n-onetoone

The documentation is very sound but I just can't seem to comprehend it. Here's what I figured out from the explanation. In my case, the Order object generates the primary key for both the Order and the PaymentInformation. The constraint="true" should be on the one-to-one mapping on the PaymentInformation object and the Id should have a generator of foreign. Am I even in the same city let alone ballpark? If so, does that mean that whenever you do a one-to-one relationship, since iit is a cyclical association, does one of them have to be lazy so that they don't keep populating each other or does Hibernate have some way of determining how deep it has to go in populating each object?




So would the mapping for the Order and PaymentInformation be as follows?

Code:

<hibernate-mapping>
  <class name="com.gpani.commerce.paytrust.beans.order.Order" table="ORDERS" dynamic-update="false" dynamic-insert="false">
    <id name="orderId" column="ORDERID" type="java.lang.Long" unsaved-value="null">
      <generator class="native"></generator>
    </id>
    <one-to-one name="paymentInformation" class="com.gpani.commerce.paytrust.beans.order.PaymentInformation" cascade="save-update" outer-join="auto" constrained="false"/>
  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="com.gpani.commerce.paytrust.beans.order.PaymentInformation" table="PAYMENTINFORMATIONS" dynamic-update="false" dynamic-insert="false">
    <id name="orderId" column="ORDERID" type="java.lang.Long" unsaved-value="null">
       <generator class="foreign"></generator>
    </id>
    <one-to-one name="order" class="com.gpani.commerce.paytrust.beans.order.Order" constrained="true"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 6:31 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Sorry for the confusion. Answered my own question. (Hated people that did that in school. So feel free to flame me.)

The only thing I had missing was the property for the Id in the PaymentInformation section.
Code:

        <id
            name="orderId"
            column="ORDERID"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="foreign">
                <param name="property">order</param>
            </generator>
        </id>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 11:45 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
Hmm, I didn't think you had to do that...and didn't know the generator could take a param node. I guess *I* should RTFM too!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 12, 2004 5:41 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
You can also pass a parameter into the generator to use sequences.


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