-->
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.  [ 2 posts ] 
Author Message
 Post subject: Frustrating doubt about maping classes in one special case
PostPosted: Sat Apr 04, 2009 8:37 pm 
Newbie

Joined: Sat Apr 04, 2009 7:34 pm
Posts: 1
Hello, I have a doubt about map some classes with Hibernate 3.3.x and I can't resolve myself. It's very frustrating... hope somebody can light me the way.

Domain description
I have 3 classes ('Company', 'Branch' and 'Employee') and these requirements:
- A company has one or more branch.
- A company has one or more employees. (Company has a set of employees)
- An employee could work in one or more branches. (A branch has a set of employees, of course these employees belongs to the company)

Classes
Code:
public class Company {
   private Set<Branch> branches = new HashSet<Branch>();
   private Set<Employee> employees = new HashSet<Employee>();
   ...
}

Code:
public class Branch {
   private Set<Employee> employees = new HashSet<Employee>();
   ...
}

Code:
public class Employee {
   ...   
}


Hibernate mapping
Code:
<class name="Company" table="COMPANIES">
   <id name="id" column="ID">
      <generator class="native"/>
   </id>
   <set name="branches" table="COMPANIES_BRANCHES" lazy="true">
      <key column="COMPANY_ID"/>
      <many-to-many column="BRANCH_ID" class="Branch"/>
   </set>
   <set name="employees" table="COMPANIES_EMPLOYEES" lazy="true">
      <key column="COMPANY_ID"/>
      <many-to-many column="EMPLOYEE_ID" class="Employee"/>
   </set>
</class>

Code:
<class name="Branch" table="BRANCHES">
   <id name="id" column="ID">
      <generator class="native"/>
   </id>
   <set name="employees" table="BRANCHES_EMPLOYEES" lazy="true">
      <key column="BRANCH_ID"/>
      <many-to-many column="EMPLOYEE_ID" class="Employee"/>
   </set>
</class>

Code:
<class name="Employee" table="EMPLOYEES">
   <id name="id" column="ID">
      <generator class="native"/>
   </id>
</class>


My problem
Well, according to my database design -structure generated by Hibernate- I could have an employee assigned to a branch that does't belong to any company. To avoid this problem maybe one solution could be having a table with composite keys like this:
Code:
CREATE TABLE `EMPLOYEES_PER_BRANCH` (
   `COMPANY_ID` bigint(20) NOT NULL,
   `BRANCH_ID` bigint(20) NOT NULL,
   `EMPLOYEE_ID` bigint(20) NOT NULL,
   PRIMARY KEY (`COMPANY_ID`,`BRANCH_ID`,`EMPLOYEE_ID`),
   CONSTRAINT `FK_COMPANY` FOREIGN KEY (`COMPANY_ID`) REFERENCES `companies` (`ID`),
   CONSTRAINT `FK_BRANCH` FOREIGN KEY (`BRANCH_ID`) REFERENCES `branches` (`ID`),
   CONSTRAINT `FK_EMPLOYEE` FOREIGN KEY (`EMPLOYEE_ID`) REFERENCES `employees` (`ID`)
   ) ENGINE=InnoDB DEFAULT CHARSET=latin1


That table could fix my problem, but I don't know how should I map my classes to have that result. I have been read all Hibernate documentation but I am still confusing.

I know very well that according Hibernate and ORM philosophy I should model my problem thinking in objects and forgetting relational tables, I am doing that, but this problem is present and I don't know how to avoid it.

Maybe I am modelling or mapping my classes in the wrong way inside *.hbm.xml files? Maybe have a table like I say it's a bad solution for my problem?.

Hope someone can help me in my doubt.

Thanks a lot to all Hibernate community!

Greetings


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 6:22 am 
Regular
Regular

Joined: Sun Aug 01, 2004 6:49 pm
Posts: 76
What is the problem to have a employee not assigned to a company? You have to catch this in your business model in general. There you can avoid to have such kind of employee. So you don't need to worry about this in your DBMS.

Beside of that I see following solution:
A employee can only be in one company. Then you don't need an extra join table for this kind of relation.
A branch can only be in one company. Then you don't need an extra join table for this kind of relation.

Then you can specify that the company property might not be null. So it is sure there is one assigned.

This results in:

class Employee {
Company company;
public Company getCompany() {
return company;
}
}


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