-->
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.  [ 12 posts ] 
Author Message
 Post subject: How to define hbm.xml?
PostPosted: Fri Nov 25, 2005 10:22 am 
Newbie

Joined: Fri Nov 04, 2005 2:49 am
Posts: 6
Hibernate version: 3
I have two tables in Oracle 10g linking via foreign key.

The ???.hbm.xml is generated myeclipseide.

How to fix this error?
Foreign key (FK9DB7177B3984DB05:FUNCTIONS [PROGRAM_HREF])) must have same number of columns as the referenced primary key (ROLE_PROGRAM [PROGRAM_HREF,ROLE_HREF,USERNM])

Thanks


<hibernate-mapping package="com.erp.hibernate.user">

<class name="Functions" table="FUNCTIONS">
<composite-id name="id" class="FunctionsKey">
<key-property name="functionHref" column="FUNCTION_HREF" type="java.lang.String"/>
<key-many-to-one name="roleProgram2" column="PROGRAM_HREF" class="RoleProgram"/>
<key-many-to-one name="roleProgram1" column="ROLE_HREF" class="RoleProgram"/>
<key-many-to-one name="roleProgram" column="USERNM" class="RoleProgram"/>
</composite-id>

<property name="messageKey" column="MESSAGE_KEY" type="java.lang.String" not-null="true" />
<property name="creationDate" column="CREATION_DATE" type="java.util.Date" not-null="true" />
<property name="createdBy" column="CREATED_BY" type="java.lang.String" not-null="true" />
<property name="lastUpdateDate" column="LAST_UPDATE_DATE" type="java.util.Date" not-null="true" />
<property name="lastUpdatedBy" column="LAST_UPDATED_BY" type="java.lang.String" not-null="true" />
</class>

</hibernate-mapping>



<hibernate-mapping package="com.erp.hibernate.user">

<class name="RoleProgram" table="ROLE_PROGRAM">
<composite-id name="id" class="RoleProgramKey">
<key-property name="programHref" column="PROGRAM_HREF" type="java.lang.String"/>
<key-many-to-one name="userRole1" column="ROLE_HREF" class="UserRole"/>
<key-many-to-one name="userRole" column="USERNM" class="UserRole"/>
</composite-id>

<property name="messageKey" column="MESSAGE_KEY" type="java.lang.String" not-null="true" />
<property name="creationDate" column="CREATION_DATE" type="java.util.Date" not-null="true" />
<property name="createdBy" column="CREATED_BY" type="java.lang.String" not-null="true" />
<property name="lastUpdateDate" column="LAST_UPDATE_DATE" type="java.util.Date" not-null="true" />
<property name="lastUpdatedBy" column="LAST_UPDATED_BY" type="java.lang.String" not-null="true" />

<set name="functionsSet" inverse="true">
<key column="USERNM"/>
<one-to-many class="Functions"/>
</set>

<set name="functions1Set" inverse="true">
<key column="ROLE_HREF"/>
<one-to-many class="Functions"/>
</set>

<set name="functions2Set" inverse="true">
<key column="PROGRAM_HREF"/>
<one-to-many class="Functions"/>
</set>
</class>

</hibernate-mapping>


CREATE TABLE role_program (
usernm VARCHAR2(8) NOT NULL,
role_href VARCHAR2(64) NOT NULL,
program_href VARCHAR2(64) NOT NULL,
message_key VARCHAR2(64) NOT NULL,
creation_date DATE NOT NULL,
created_by VARCHAR2(8) NOT NULL,
last_update_date DATE NOT NULL,
last_updated_by VARCHAR2(8) NOT NULL,
CONSTRAINT pk_role_program primary key (usernm, role_href, program_href),
CONSTRAINT fk_role_program foreign key (usernm, role_href) REFERENCES user_role (usernm, role_href)
) ;

CREATE TABLE functions (
usernm varchar(8) NOT NULL,
role_href VARCHAR(64) NOT NULL,
program_href VARCHAR(64) NOT NULL,
function_href VARCHAR(64) NOT NULL,
message_key VARCHAR(64) NOT NULL,
creation_date DATE NOT NULL,
created_by VARCHAR(8) NOT NULL,
last_update_date DATE NOT NULL,
last_updated_by VARCHAR(8) NOT NULL,
CONSTRAINT pk_functions primary key (usernm, role_href, program_href, function_href),
CONSTRAINT fk_functions foreign key (usernm, role_href, program_href) REFERENCES role_program (usernm, role_href, program_href)
) ;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
seems like they don't support composite keys properly.

either manually updated the generated hbm.xml or simply try hibernate tools - It should work there (at least we have a unit test for a similar case that works)

let me know how it goes ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 2:49 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Yes. Should'nt your "<set>" contain multiple "key" elements 1 per column that corresponds to the composite primary key of the FUNCTIONS class?

Code:
<set name="functionsSet" inverse="true">
<key column="USERNM"/>
<key column="ROLE_HREF"/>
<key column="PROGRAM_HREF"/>
<one-to-many class="Functions"/>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 11:39 pm 
Newbie

Joined: Fri Nov 04, 2005 2:49 am
Posts: 6
The primary key of my table composes of two or more string fields.
I do not think native is a suitable selection of ID generator.


What is the proper selection of ID generator?

Does it need to implement the hashCode for string fields primary key?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 1:28 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
try 'assigned' ... that means you have to set the values before you pass it to Session.save() .


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 1:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Key generator should be assigned and yes the key class should have equals and hashcode implemented.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 3:04 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
and it should say "implements Serializable" ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 9:41 am 
Newbie

Joined: Fri Nov 04, 2005 2:49 am
Posts: 6
Table key consists string fields.
How to implement calculation of string into hashCode?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 10:06 am 
Newbie

Joined: Fri Nov 04, 2005 2:49 am
Posts: 6
anandbn wrote:
Yes. Should'nt your "<set>" contain multiple "key" elements 1 per column that corresponds to the composite primary key of the FUNCTIONS class?

Code:
<set name="functionsSet" inverse="true">
<key column="USERNM"/>
<key column="ROLE_HREF"/>
<key column="PROGRAM_HREF"/>
<one-to-many class="Functions"/>
</set>


I have defined the above codings in header hbm.xml.
(myeclipseide highlight errors in the <set>)

How to modify the following detail hbm.xml so that it matches its header hbm.xml?

<composite-id name="id" class="FunctionsKey">
<key-property name="functionHref" column="FUNCTION_HREF" type="java.lang.String"/>
<key-many-to-one name="roleProgram2" column="PROGRAM_HREF" class="RoleProgram"/>
<key-many-to-one name="roleProgram1" column="ROLE_HREF" class="RoleProgram"/>
<key-many-to-one name="roleProgram" column="USERNM" class="RoleProgram"/>
</composite-id>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 11:26 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Yup. I guess the compoiste id defintion looks correct.

For hashcode() implementation the simplest thing would be to sumthe hash code of your individual strings and return that back.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 12:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hashcode etc info http://www-128.ibm.com/developerworks/java/library/j-jtp05273.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 5:37 am 
Newbie

Joined: Fri Nov 04, 2005 2:49 am
Posts: 6
I have difficulty in defining foreign key linking between two tables the hbm.xml files. The error message is "FOREIGN...same number of columns..."
How can I define the hbm.xml properly so that it reflects that the table has primary key made of strings fields and linking to other table by foreign key.
I think it should be very common in the design of table structure.
Any idea to write hbm.xml files properly?


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