-->
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.  [ 6 posts ] 
Author Message
 Post subject: composite-id relations problems
PostPosted: Thu Apr 29, 2004 9:56 am 
Newbie

Joined: Thu Apr 29, 2004 9:25 am
Posts: 6
Location: Castellon - Spain
Hello,
I've read the "Hibernate Reference Documentation" and I've tried the example of the chatpter 18 about the relation "Customer/Order/Product" and it worked. But in my database the primary keys of tables are composited, so I appended a new field COMPANY_CODE to the table ORDER and to the table CUSTOMER, and this field is part of the primary key. When I relatione orders with customers this don't work. This is my status :

CUSTOMERS :
COMPANY_CODE
CUSTOMER_ID
CUSTOMER_NAME

ORDERS :
COMPANY_CODE
ORDER_ID
CUSTOMER_ID
DATE



<hibernate-mapping>

<class name="business.Customer" table="CUSTOMERS">
<composite-id>
<key-property name="companyCode" column="COMPANY_CODE"/>
<key-property name="customerId" column="CUSTOMER_ID"/>
</composite-id>

<property name="name"/>

<set name="orders" inverse="true" lazy="true">
<key>
<column name="COMPANY_CODE"/>
<column name="CUSTOMER_ID"/>
</key>
<one-to-many class="business.Order"/>
</set>

</class>

<class name="business.Order" table="ORDERS">

<composite-id>
<key-property name="companyCode" column="COMPANY_CODE"/>
<key-property name="orderId" column="ORDER_ID"/>
</composite-id>

<property name="date"/>

<!-- HERE IS THE PROBLEM -->

<many-to-one name="customer">
<column name="COMPANY_CODE"/>
<column name="CUSTOMER_ID"/>
</many-to-one>

<!-- END HERE IS THE PROBLEM -->

</class>


</hibernate-mapping>


The error that I get is this :

INFO - Hibernate 2.1.3
INFO - hibernate.properties not found
INFO - using CGLIB reflection optimizer
INFO - configuring from resource: /hibernate.cfg.xml
INFO - Configuration resource: /hibernate.cfg.xml
INFO - Mapping resource: Ordenes.hbm.xml
INFO - Mapping class: business.Customer -> CUSTOMERS
INFO - Mapping class: business.Order -> ORDERS
INFO - Configured SessionFactory: null
INFO - processing one-to-many association mappings
INFO - Mapping collection: business.Customer.orders -> ORDERS
INFO - processing one-to-one association property references
INFO - processing foreign key constraints
INFO - Using dialect: net.sf.hibernate.dialect.MySQLDialect
INFO - Use outer join fetching: false
WARN - No connection properties specified - the user must supply JDBC connections
INFO - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
INFO - Use scrollable result sets: false
INFO - Use JDBC3 getGeneratedKeys(): false
INFO - Optimize cache for minimal puts: false
INFO - echoing all SQL to stdout
INFO - Query language substitutions: {}
INFO - cache provider: net.sf.ehcache.hibernate.Provider
INFO - instantiating and configuring caches
INFO - building session factory
net.sf.hibernate.MappingException: Repeated column in mapping for class business.Order should be mapped with insert="false" update="false": COMPANY_CODE at net.sf.hibernate.persister.AbstractEntityPersister.checkColumnDuplication(AbstractEntityPersister.java:1002)
at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:819)
at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:42)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:768)
at SelectOrder.main(SelectOrder.java:9)
Exception in thread "main"



I'm trying some mappings but it doesn't work.
Can anybody tell me how I must make this type of relation
Thank you very much.

Sorry, my english is very bad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 11:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
should use <key-many-to-one ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 11:39 am 
Newbie

Joined: Thu Apr 29, 2004 9:25 am
Posts: 6
Location: Castellon - Spain
delpouve wrote:
should use <key-many-to-one ...


Thank you, but sorry, in this case I think that solution isn't correct. Also I tried it and doesn't work.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can't map that. This may lead to incossistence if the many-to-one and the composite id you set does not share the same COMPANY_CODE

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 02, 2004 7:31 am 
Newbie

Joined: Thu Apr 29, 2004 9:25 am
Posts: 6
Location: Castellon - Spain
Can't I map this ??? I have Legacy DataBase and all tables has this type of relation. So, can not use Hibernate with my DataBase System ??

Thanks for your replies


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 03, 2004 10:22 am 
Newbie

Joined: Mon May 03, 2004 9:29 am
Posts: 2
Perhaps you can do this. I have similar situation and had the same exception. The exception say only you must use unsert="false" and update="false" in your many-to-one definition. My is following: i have Product with "productKey" and simple int id, Language with "languageKey" as id and one join table ProductLocal with composite primary key (language_fk, product_fk) and one nvarchar column for localized product name. For ProductLocal i use component as composite-id as described in chapter 7.4 of reference.

Here are my mappings:
<class name="Language" table="Language" dynamic-update="false" dynamic-insert="false" mutable="true" >

<id name="languageKey" column="language_id" type="string" length="2" >
<generator class="assigned"></generator>
</id>
</class>


<class name="Product" table="Product" dynamic-update="false" dynamic-insert="false" mutable="true" >

<id name="id" column="product_id" type="java.lang.Integer" >
<generator class="hilo"></generator>
</id>

<property name="productKey" type="java.lang.String" update="true" insert="true" column="product_key" length="20" not-null="true" unique="true" />
...

</class>


<class name="ProductLocal" table="ProductLocal" dynamic-update="false" dynamic-insert="false" mutable="true" >

<composite-id name="id" class="ProductLocalID">
<key-property name="languageID" type="java.lang.String" column="language_fk" />
<key-property name="productID" type="int" column="product_fk" />
</composite-id>

<many-to-one name="language" class="Language" cascade="none" outer-join="auto" update="false" insert="false" column="language_fk" not-null="true" />

<property name="localization" type="java.lang.String" update="true" insert="true" >
<column name="localization" length="100" not-null="true" sql-type="nvarchar(100)" />
</property>

<many-to-one name="product" class="Product" cascade="none" outer-join="auto" update="false" insert="false" column="product_fk" not-null="true" />

</class>

As i had no update="false" and insert="false" in my many-to-one's in ProductLocal i had the same exception. But yous example is a bit diffrent - probably you must change orders primary key:

<composite-id>
<key-property name="companyCode" column="COMPANY_CODE"/>
<key-property name="customerId" column="CUSTOMER_ID"/>
<key-property name="orderId" column="ORDER_ID"/>
</composite-id>

So if you set primary key for one order-object both column needed for many-to-one has values. If you say insert="false" and update="false" it means if you set customer with aOrder.setCustomer(aCustomer) the values from the customer (the customers primary key - "COMPANY_CODE and "CUSTOMER_ID") will not be used for insert statement - they must come from anywhere else - from composite-id for example you must set in any case.

Hopefully it helps.


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