-->
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.  [ 11 posts ] 
Author Message
 Post subject: HBM2JAVA with composited PK
PostPosted: Mon Jun 21, 2004 4:02 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Database: MySQL4.0.20a under Windows XP
I am using Hibernate 2.1.4 and Hibernate-Extensions-2.1.2

Middlegen-Hibernate-r4 to generate the HBM.XML . The generated Mapping file as follow:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.test.Element"
table="element"
>

<composite-id name="comp_id" class="com.test.ElementPK">
<key-property
name="eid"
column="EID"
type="java.lang.Long"
length="10"
/>
<!-- bi-directional many-to-one association to Structure -->
<key-many-to-one
name="structure"
class="com.test.Structure"
>
<column name="SID" />
</key-many-to-one>
</composite-id>

<property
name="ename"
type="java.lang.String"
column="EName"
not-null="true"
length="15"
/>
<property
name="elesize"
type="java.lang.Integer"
column="Elesize"
length="5"
/>
<property
name="description"
type="java.lang.String"
column="Description"
length="20"
/>

<!-- associations -->
<!-- bi-directional one-to-many association to Elementpara -->
<set
name="elementparas"
lazy="true"
inverse="true"
>
<key>
<column name="EID" />
<column name="SID" />
</key>
<one-to-many
class="com.test.Elementpara"
/>
</set>
<!-- bi-directional one-to-many association to Elementlist -->
<set
name="elementlists"
lazy="true"
inverse="true"
>
<key>
<column name="EID" />
<column name="SID" />
</key>
<one-to-many
class="com.test.Elementlist"
/>
</set>

</class>
</hibernate-mapping>

when I generate the Java from those hbm.xml files(My talbe containted composited PrimaryKey.) by using hbm2java.bat , I got the warning say: No properties has been marked as being used in equals/hashcode for TableName. Using object identifier whisch is RARELY safe to use! See http://hibernate.org/109.html. This warning is for Element.java and ElementPK.java as well. Could anybody let me know what's the meaning of this warning? And how to fix it?

And why generate 2 java files(Element.java & ElementPK.java) when I have composite PK in my talbe.

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 9:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
HBM2Java recently has changes in the generation of equals and hash code methods. Now a composite key based class should have theses fields generated. HBM2Java is complaining that it there are no (new) meta tags to tell it to, 1) generate the equals etc, and 2) which fields to use in this process.

Thus new meta tags have been introduced, eg,
implement-equals include a equals() and hashCode() method in this class.
use-in-equals include this property in the equals() and hashCode() method.

You have several options:
1) Use an older version of hbm2java which generates then equals hashcode by default (though you need to verify what is created is OK).

2) Manually add the meta tags into the mapping documents. Middlegen R5 will include support for these tags. Its not to far off.

3) Ignore the warning and add you own methods to the generated java class.

You second question.
Middlegen generates Composite keys mappings to work with a separate class for the primary key by default. This is the recommended approach. Alternatively, you can select the option to generate the mapping as an integrated composite key.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 6:05 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Thank you for your quick reply.

I changed my database tables and now every table just has one primary key (I set the artificial primary key to them). Do you think this is a good approach?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 6:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes, this is 100% a very good approach. Use auto-generated keys without business meaning wherever possible. And avoid composite-keys.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 7:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Yes 100% the best approach. It's interesting you selected the option that I wanted to include but decided not to since many people don't have the choice.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 9:55 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Haha, Others don't want to take this choice just because it is not a easy job if working on a huge database. Luckly I just have the small one.

But I still got the problem to let the JAVA code Hibernate generated to work, so sad~~~ How to insert data and why I got unmapping class error?

Here is two of my mappling files generated by the Middlgen.

User.hbm.xml
-
<hibernate-mapping>
-
<!--

Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
-
<class name="test.User" table="users">
-
<id name="uno" type="java.lang.Integer" column="Uno">
<generator class="assigned"/>
</id>
<property name="uname" type="java.lang.String" column="UName" not-null="true" length="25"/>
<!-- associations -->
<!-- bi-directional one-to-many association to Model -->
-
<set name="models" lazy="true" inverse="true">
-
<key>
<column name="Uno"/>
</key>
<one-to-many class="test.Model"/>
</set>
</class>
</hibernate-mapping>

_________________________________


Model.hbm.xml


<hibernate-mapping>
-
<!--

Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
-
<class name="test.Model" table="model">
-
<id name="mid" type="java.lang.Long" column="MID">
<generator class="assigned"/>
</id>
<property name="ctime" type="java.sql.Timestamp" column="CTime" not-null="true" length="19"/>
<!-- associations -->
<!-- bi-directional many-to-one association to User -->
-
<many-to-one name="user" class="test.User" not-null="true">
<column name="Uno"/>
</many-to-one>
-
<!--
bi-directional one-to-many association to Structurelist
-->
-
<set name="structurelists" lazy="true" inverse="true">
-
<key>
<column name="MID"/>
</key>
<one-to-many class="test.Structurelist"/>
</set>
-
<!--
bi-directional one-to-many association to Simulation
-->
-
<set name="simulations" lazy="true" inverse="true">
-
<key>
<column name="MID"/>
</key>
<one-to-many class="test.Simulation"/>
</set>
</class>
</hibernate-mapping>

______________________________

When i try to insert the data to the user table , I got 2 problems, one is cannot insert , the other is unmapping class test.Model.

If anybody can help? Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 3:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Your missing mapping(s), eg, Model has a set reference to Structurelist. So hibernate is unable to map model therefore User cannot be used since it references Model.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 1:38 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Sorry, I couldn't understand what you mean. And acuatlly, I also have structurelist mapping file. And I generate all the Java code from files.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 8:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Take it step by step - try commenting out some of the relationships from the mapping documents so the depencies are smaller and save from there (obviously you may need to add property references for not-null fields that were in a relationship). Once a simple mapping is working then work forward from there by introducing one more etc etc. This will isolate the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 7:09 pm 
Newbie

Joined: Mon Jun 21, 2004 3:43 pm
Posts: 19
Thank you so much for your help, I found my problem, just miss one .addclass() , ^^'

Now I am going to do some query in my DB (HQL tutorial), Do you know any good example? Let me know some link, thank you once again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 11:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You could look at the tools tutorial since it has a section on doing queries with hbern8ide.
http://hibernate.bluemars.net/159.html
The last entry.


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