• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused.. When to use What in Hibernate?

 
Clarence J M Tauro
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a bit confused with the following in Hibernate. I am using Hibernate 3.2

Consider a scenario like this

class Address{
..
..
}

class Employee{
..
..
Set<Address> addresses;
}

I can insert into the database by using the following

<set name="companies" table="companyTable">
<key column="employeeId" />

<composite-element class="Company">
<property name="companyName" column="companyName" not-null="true" />
<property name="companyHeadOffice" column="companyHeadOffice" not-null="true" />
<property name="companyYearOfEstablishment" column="companyYearOfEstablishment" not-null="true" />
</composite-element>
</set>


OR

I can use

<set name="address" cascade="all">
<key column="employeeId" />
<one-to-many class="Address" />
</set>

and use 2 hbm files i.e. address.hbm.xml and employee.hbm.xml

My question is when to use what? Both does the same thing right [might be I am wrong in some sense].
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<composite-element /> is used to define a many to many association where there are extra properties in the association. Just using a collection is a normal one to many association, so these mappings have different purposes. Is the cardinality of the association between company and address one to many or many to many?
 
Clarence J M Tauro
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Employee and Address had One-To-Many association in my scenerio.

So can we conclude that Set and One-To-Many can be used Interchangeably? If so, is there any performance issues?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic