-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to-one | many-to-one questions.
PostPosted: Wed Jun 04, 2008 11:12 am 
Newbie

Joined: Wed Jun 04, 2008 10:28 am
Posts: 4
Hi,

sorry for my english! :)

I read an example in the hibernate documentation how to use one-to-one and many-to-one.



Quote:
<hibernate-mapping>

<class name="Parent">
<id name="id">
<generator class="sequence"/>
</id>
<set name="children">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
</class>

<class name="Child">
<id name="id">
<generator class="sequence"/>
</id>
<property name="name"/>
</class>

</hibernate-mapping>


I can't understand what, in my project, insert in <set name="??? "> .
If I put a random values encounter an error like this:
"Could not find a getter for <random values> in class com.test.User.

I can insert only property name present in the same hbm.xml file and if i put them encounterd an error like this: "Duplicate property mapping of usrIdfound in <Class name>.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 5:24 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
you need to generate getter/setter for the collection in com.test.User.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 11:15 am 
Newbie

Joined: Wed Jun 04, 2008 10:28 am
Posts: 4
Ok, but what should contain the new collection and what would be its purpose?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 11:33 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, your lack of setters and getters is really causing you some problems.

Here's a tip - if you're just starting out, think about using JPA annotations. Here are some really simple examples that will make learning Hibernate alot easier for you:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=17mappingonetooneassociations

Good luck!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 4:51 am 
Newbie

Joined: Wed Jun 04, 2008 10:28 am
Posts: 4
Thank you, but am a little confused.
I am using Struts and I believed that to make join i need to modify the file <class>. Hbm.xml


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 5:03 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
yes using jpa is great.
It's simple when you define an attribute in your mapping file you always need the corresponding getter and setter methods so hibernate can fill/get the values.

so if you for example map a string with name="username" in class="model.User" you have to add in your class(User) getUsername() and setUsername() that it works.

You also need an standard constructor like
public void User(){}.

maybe you can post the code of your class here and the corresponding mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 5:26 am 
Newbie

Joined: Wed Jun 04, 2008 10:28 am
Posts: 4
Ok.
But I wonder why if I enter a property hibernate tells me that the property is duplicated.
Quote:
<class name="com.Test.User" table="user" >
<id name="usrName" type="java.lang.String">
<column name="usr_name" length="20" />
<generator class="increment" />
</id>

<property name="usrId" type="java.lang.Integer">
<column name="usr_ID" not-null="true" />
</property>


usrName is the PK.
usrId is the FK.

In another class:
Quote:
<class name="com.Table.Main" table="main">
<id name="mainId" type="java.lang.Integer">
<column name="main_ID" />
<generator class="increment" />
</id>

<property name="mainUsrId" type="java.lang.Integer">
<column name="main_usrID" not-null="true" />
</property>



mainId is the PK.
mainUsrId is the FK.

relations are Main.mainUsrID ----N----------1---->User.usrId.

if I follow the example of the first post how should I write this parts:

Quote:
<set name="children">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>


and

Quote:
<many-to-one="????">


If i add those lines the error is the same: Duplicate property mapping.

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 6:29 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
hm i meant the java classes actualy but anyway, i'm not that familiar with xml mappings cause i use jpa but on the Parent side you need a Collection (Set) which contains the children. And on the Children side you can use the many-to-one to have a reference to the corresponding Parent.


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