-->
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.  [ 9 posts ] 
Author Message
 Post subject: How should I do this mapping?
PostPosted: Wed May 12, 2004 9:38 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Hi, first of all sorry for this newbish question... but i've got to know...

I've got 4 tables
Code:
COMPANY
-----------
companyID (pk)
name
address
zipcode
city
telephone
fax
email
vat
bankaccount
website


USERACCOUNT
------------------
userID (pk)
firstname
lastname
address
zipcode
city
telephone
email
username
password
last_login
status
companyID (fk to company)


USERGROUP
--------------
id (pk)
userID (fk to useraccount)
groupID (fk to group)


GROUP
--------
groupID (pk)
name
description


So what should my mapping files look like?

Suppose I want to insert a useraccount, and in the form I select the right company. How should this be done in java code?

Thanks to you all for helping me with this!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 9:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Company 1 -- * userAccount 1 -- * UserGroup * -- 1 Group

where 1 -- * means one to many
and * -- 1 means many to one

but that is just ONE solution, you have to choose how you will "travel" your object graph and decide if you need bidirectionnal assocations


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 3:23 pm 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Yes, I know how the relationship is..

Could you give me an example of the mapping file?

but suppose I want to insert a useraccount, and in the form I select the right company. How should this be done?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 4:17 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
Could you give me an example of the mapping file?


Could you please read the doc, try to do it by yourself, and if it doesn't work post what you've written all i'll be happy to help you then, don't forget that most of us are working...


Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2004 3:53 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
COMPANY MAPPING FILE: Company.hbm.xml
Code:
<hibernate-mapping>
<class name="Company" table="COMPANY">
  <id column="companyID" name="id" type="integer">
   <generator class="increment"/>
  </id>
  <property name="name" type="string"/>
  <property name="address" type="string"/>
  <property name="zipcode" type="string"/>
  <property name="city" type="string"/>
  <property name="telephone" type="string"/>
  <property name="fax" type="string"/>
  <property name="email" type="string"/>
  <property name="vat" type="string"/>
  <property name="bankaccount" type="string"/>
  <property name="website" type="string"/>
  <set name="users" table="USERACCOUNT">
   <key column="companyID"/>
   <one-to-many class="UserAccount"/>
  </set>
</class>
</hibernate-mapping>


USERACCOUNT MAPPING FILE: UserAccount.hbm.xml
Code:
<hibernate-mapping>
<class name="UserAccount" table="USERACCOUNT">
  <id column="userID" name="id" type="integer" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property name="firstname" type="string"/>
  <property name="lastname" type="string"/>
  <property name="address" type="string"/>
  <property name="zipcode" type="string"/>
  <property name="city" type="string"/>
  <property name="telephone" type="string"/>
  <property name="email" type="string"/>
  <property name="username" type="string"/>
  <property name="password" type="string"/>
  <property name="old_password" type="string"/>
  <property name="last_login" type="date"/>
  <property name="status" type="string"/>
  <!--<property name="companyID" type="integer"/>-->
  <many-to-one column="companyID" name="company"/>
 
  <set name="groups" table="USERGROUP">
     <key column="userID"/>
     <many-to-many column="groupID" class="Group"/>
  </set>
 
</class>
</hibernate-mapping>


GROUP MAPPING FILE: Group.hbm.xml
Code:
<hibernate-mapping>
<class name="Group" table="GROUP">
  <id column="groupID" name="id" type="int">
   <generator class="increment"/>
  </id>
  <property name="name" type="string"/>
  <property name="description" type="string"/>
 
  <set name="users" table="USERGROUP">
     <key column="groupID" />
     <many-to-many class="UserAccount" column="userID"/>
  </set>
 
</class>
</hibernate-mapping>


Please correct me if I made mistakes.. Or else tell me that I did a good job :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:02 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Is it that difficult to determine if my mapping files are correct or not?
Do you need more information?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The problem is: It's your job.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:10 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
christian wrote:
The problem is: It's your job.

Yes, I know.. that's why I did it my why. But I only want to know if that, what I've created, is correct or not...

Or even.. In what why I should improve it...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:15 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The mapping looks good, but you have to add inverse="true" to one of the <set>s you use for the many-to-many association. Doesn't matter which side.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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