-->
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: want many-to-one column to be in compsite pk.
PostPosted: Thu Jun 16, 2005 2:23 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.3
Mapping documents:
<!-- InventoryTransDetailsInfo start-->
<class name="com.vertical.accelere.inventory.InventoryTransDetailsInfo" table="inventory_transaction_details">
<composite-id>
<key-property name="itemCode" column="item_code" type="int"/>
</composite-id>

<property name="price" column="price" type="double"/>
<property name="quantity" column="quantity" type="double"/>
<many-to-one name="itInfo" column="transaction_no" class="com.vertical.accelere.inventory.InventoryTransInfo"></many-to-one>
</class>
<!-- InventoryTransDetailsInfo end-->

<!-- InventoryTransactionInfo start-->

<class name="com.vertical.accelere.inventory.InventoryTransInfo" table="inventory_transaction">
<id name="transactionNumber" type="int" column="transaction_no">
<generator class="increment" />
</id>
<property name="transactionType" type="string" column="transaction_type"/>
<bag name="itdInfo" lazy="true" inverse="true" cascade="save-update" >
<key column="transaction_no"></key>
<one-to-many class="com.vertical.accelere.inventory.InventoryTransDetailsInfo"/>
</bag>
</class>
<!-- InventoryTransactionInfo end-->

Name and version of the database you are using:
PostgreSQL 8
The generated SQL (show_sql=true):
Hibernate: update inventory_transaction set transaction_type=? where transaction_no=?
Hibernate: update inventory_transaction_details set price=?, quantity=?, transaction_no=? where item_code=?

Hi

I have made one-to-many association between two table Inventory_transaction and Inventory_transaction_details tables.

Below is the generated SQL for Inventory_transaction_details table.
Hibernate: update inventory_transaction_details set price=?, quantity=?, transaction_no=? where item_code=?
But i want the query to be
Hibernate: update inventory_transaction_details set price=?, quantity=? where item_code=? and transaction_no=?

i.e i want item_code and transaction_no to be composite Primary key Inventory_transaction_details table.

How can i do that in the mapping file.

Help me friends.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 3:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
add the column to the composite key as either a key-many-to-one or simply as a simple property and keep the many-to-one but set insert/update="false" since you already mapped it in the id.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 4:07 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Thanks Max

I got it working Properly

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: composite key mapping using many-to-one
PostPosted: Wed Aug 17, 2005 9:06 am 
Newbie

Joined: Sun Aug 14, 2005 12:25 am
Posts: 5
I'm curious how you fix this. I have a similar problem.

two tables

tbl_user
tbl_user_phone

below is the user_phone table definition

reate table tbl_user_phone(
col_user_id INT NOT NULL, CONSTRAINT tbl_user_phone_col_user_id_fk foreign key(col_user_id) references tbl_user(col_id),
col_phone char(10),
col_prefered boolean,
);

the column col_user_id is a foreign key to tbl_user and can allow for one to many relationships.

I've tried to map this similar to yours and I get a very similar error. Below is my mapping

<class name="edu.cmu.ua.dbo.UserPhone" table="tbl_user_phone" schema="public">

<id name="userId" column="col_user_id">
<generator class="assigned"/>
</id>

<many-to-one
name="userId"
column="col_user_id"
class="edu.cmu.ua.dbo.User"
update="false"
insert="false"
/>

And this is my mappine in User.hbm.xml

<set name="phones" >
<key column="col_user_id" not-null="true"/>
<one-to-many class="edu.cmu.ua.dbo.UserPhone"/>
</set>


I took the poster's suggestion of using update/insert="false" but I still get the error.

org.hibernate.MappingException: Repeated column in mapping for entity: edu.cmu.ua.dbo.UserPhone column: col_user_id (should be mapped with insert="false" update="false")

Ideas?


Top
 Profile  
 
 Post subject: Solution
PostPosted: Wed Aug 17, 2005 10:45 am 
Newbie

Joined: Sun Aug 14, 2005 12:25 am
Posts: 5
This was the solution to my problem. I'm posting in order to help others who may encounter this problem/error.

Since UserPhone is an extension of the User and won't be shared betweeen other POJO's it made sense to include this as part of a User's set.

I removed the mapping UserPhone.hbm.xml and included the following in the User.hbm.xml

<set name="phones" table="tbl_user_phone">

<key column="col_user_id"/>

<composite-element class="edu.cmu.ua.dbo.UserPhone">
<parent name="User"/>
<property name="Phone" >
<column name="col_phone" not-null="true"
unique="true"/>
</property>

<property name="Prefered" type="boolean">
<column name="col_prefered" />
</property>
</composite-element>
</set>

This works well and gives me the table structure that I desire. SchemaExport generates this table from the above mapping:


[schemaexport] create table tbl_user_phone (
[schemaexport] col_user_id int4 not null,
[schemaexport] col_phone varchar(255) not null unique,
[schemaexport] col_prefered bool,
[schemaexport] primary key (col_user_id, col_phone)

[schemaexport] alter table tbl_user_phone
[schemaexport] add constraint FK999B3B1B7EEAD1F
[schemaexport] foreign key (col_user_id)
[schemaexport] references public.tbl_user;

Now I'm going to add a phone type table and link the type of phone from the user_phone table. The above <Set> can accomodate this because of the <nested-composite-element> tag.


I hope this helps anyone who may have a similar problem/situation.


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.