-->
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 frustration
PostPosted: Wed Oct 20, 2004 2:21 am 
Newbie

Joined: Wed Oct 20, 2004 2:02 am
Posts: 4
Hibernate version: 2.1.2

I am trying to implement a one-to-one relationship. A row gets inserted into the "products" table, but not in the "products_one" table.

class Product
{
String id
String name;
double price
ProductOne productOne
}

class ProductOne
{
String id
String aliasname
Product product;
}

Mapping documents:
Product Mapping:

<hibernate-mapping>

<class name="test.hibernate.Product" table="products">


<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>

</id>

<property name="name">
<column name="name" sql-type="char(255)" not-null="true"/>
</property>

<property name="price">
<column name="price" sql-type="double" not-null="true"/>
</property>

<one-to-one
name="productOne"
class="test.hibernate.ProductOne">
</one-to-one>
</class>
</hibernate-mapping>

ProductOne mapping:

<hibernate-mapping>

<class name="test.hibernate.ProductOne" table="products_one">

<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="foreign">
<param name="property">product</param>
</generator>
</id>

<property name="aliasname">
<column name="aliasname" sql-type="char(25)" not-null="true"/>
</property>

<one-to-one
name="product"
class="test.hibernate.Product">
</one-to-one>

</class>

</hibernate-mapping>



Setting up the objects

Product p = new Product();
p.setName(args[0]);
p.setAmount(Integer.parseInt(args[1]));
p.setPrice(Double.parseDouble(args[2]));

//init ProductOne
ProductOne po = new ProductOne();
po.setAliasname("Test");
po.setProduct(p);

p.setProductOne(po);

Code between sessionFactory.openSession() and session.close():
// save product and close session
Transaction t = sess.beginTransaction();
sess.save(p);
t.commit();
sess.close();



Please lend a hand. Like I said, rows are inserted into the products table, but not the "child" table products_one.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 20, 2004 3:53 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
Use the many-to-one in one direction and then one-to-one in the reverse direction as suggested in Chapter 6 of Hibernate in Action.

For example:
Code:
<class name="test.hibernate.Product" table="products">
....
  <many-to-one name="productOne"
    class="test.hibernate.ProductOne"
    column="PRODUCT_ONE_ID"
    cascade="save-update"
    unique="true"/>
....
</class>


<class name="test.hibernate.ProductOne" table="products_one">
....
  <one-to-one name="product"
    class="test.hibernate.Product"
    property-ref="productOne"/>
....
</class


This is also described in more detail in section 5.1.11 in the Hibernate Reference, and is refered to as "one-to-one with foreign key constraint".

Notice the cascade="save-update" on the Product side, so that when you save instances of Parent, its associated ProductOne gets saved also.

Hope this helps,
Kevin Hooke


Top
 Profile  
 
 Post subject: if it is single connection ,what can I do?
PostPosted: Thu Oct 21, 2004 3:43 am 
Newbie

Joined: Tue Feb 24, 2004 11:43 pm
Posts: 4
I don't want to add anything to <one to one> for it will limit the class of container.

_________________
I'm a beginner.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 10:40 am 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
These are quotes from hibernate's reference: (5.1.11. one-to-one)

Quote:
There are two varieties of one-to-one association:

primary key associations

unique foreign key associations (khooke's idea)


If you want to cascade the save you have to specify the cascade type:
Code:
cascade="all|none|save-update|delete"


Code:
<one-to-one
name="productOne"
class="test.hibernate.ProductOne">
cascade="...choice is yours:)..."
</one-to-one>


Lukasz

PS. use 'code' tag around your code - it is easier to read


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.