-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping problem: company => person => function
PostPosted: Fri Aug 03, 2007 9:04 am 
Newbie

Joined: Wed Aug 01, 2007 4:53 pm
Posts: 11
This is what i have:

Companies (id, name,...)
persons (id, name,....)
functions (id,description)
company_person(id,com_id,prs_id,fun_id)

i want to use this in my jsf application but having problems modeling it...

I want the company to have a methode wherei get all the persons with there function in that company. This is what i have so far:

Code:
<hibernate-mapping>
<class name="Company" table="companies">
  <id name="id">
   <generator class="identity"/>
  </id>
  <set name="persons" table="company_person">
     <key column="com_id"/>
     <many-to-many class="Person" column="prs_id"/>
   </set>
</class>
</hibernate-mapping>


Here is the problem... i guess when i fetch the set of persons, i don't have the functions...
My person file is like this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Person" table="persons">
       <id name="id">
      <generator class="identity"/>
       </id>
       <property name="name"/>
</class>
</hibernate-mapping>


As you can see, a person don't know what function he is because he can be part of more than one company and have a different function in a different company.

So.. i do i tell hibernate this and in what file do i have to define the relation between function and person. and how...

My function file is like this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="Function" table="functions">
   <id name="id">
      <generator class="identity"/>
   </id>
   <property name="description"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 04, 2007 4:16 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I suggest you create a new entity: employees, as "person with function at company" and have a set of these in your company object rather than a set of persons.

Assuming a person can only have 1 function at a company:

Employee.java
Code:
package test.passero;

public class Employee {
   private Long id;
   private Person person;
   private Function function;
   
   public Employee() {}
   public Employee(Person person, Function function) {
      this.person = person;
      this.function = function;
   }
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   public Person getPerson() {
      return person;
   }
   public void setPerson(Person person) {
      this.person = person;
   }
   public Function getFunction() {
      return function;
   }
   public void setFunction(Function function) {
      this.function = function;
   }
}


Employee.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.passero">

<class name="Employee" table="passero_employee">
   <id name="id">
      <generator class="native"/>
   </id>
   <many-to-one name="person" column="person_id" not-null="true"/>
   <many-to-one name="function" column="function_id" not-null="true"/>
</class>
</hibernate-mapping>


Company.java
Code:
package test.passero;

import java.util.Set;

public class Company {

   private Long id;
   private String name;
   private Set<Employee> employees;

   public Company() {}
   public Company(String name) {
      this.name = name;
   }
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Set<Employee> getEmployees() {
      return employees;
   }
   public void setEmployees(Set<Employee> employees) {
      this.employees = employees;
   }
}


Company.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.passero">

<class name="Company" table="passero_company">
   <id name="id">
      <generator class="native"/>
   </id>
   <property name="name"/>
   <set name="employees" cascade="all, delete-orphan">
      <key column="company_id"/>
      <one-to-many class="Employee"/>
   </set>
</class>
</hibernate-mapping>


TestPassero.java
Code:
package test.passero;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class TestPassero extends TestCase {

   public void testPassero() {
      createCompanies();
      
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      Company redHat = (Company)s.createCriteria(Company.class)
      .add(Restrictions.eq("name", "Red Hat"))
      .uniqueResult();
      
      assertNotNull(redHat);
      assertNotNull(redHat.getEmployees());
      assertEquals(2, redHat.getEmployees().size());
      for (Iterator<Employee> iterator = redHat.getEmployees().iterator(); iterator.hasNext();) {
         Employee employee = (Employee) iterator.next();
         assertNotNull(employee.getFunction());
         assertNotNull(employee.getPerson());
         assertNotNull(employee.getPerson().getName());
         if (employee.getPerson().getName().startsWith("mike")) {
            assertEquals("developer", employee.getFunction().getDescription());
         } else if (employee.getPerson().getName().startsWith("passero")) {
            assertEquals("consultant", employee.getFunction().getDescription());
         } else {
            fail("unexpected employee name: "+employee.getPerson().getName());
         }
      }

      s.getTransaction().commit();
      s.close();
   }
   
   private void createCompanies() {
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      Function director = new Function("director");
      s.save(director);
      Function consultant = new Function("consultant");
      s.save(consultant);
      Function developer = new Function("developer");
      s.save(developer);

      Person mike = new Person("mike");
      s.save(mike);
      Person passero= new Person("passero");
      s.save(passero);

      Company microsoft = new Company("Microsoft");
      Employee mikeConsultantAtMicrosoft = new Employee(mike, consultant);
      Employee passeroDirectorAtMicrosoft = new Employee(passero, director);
      Set<Employee> microsoftEmployees = new HashSet<Employee>();
      microsoftEmployees.add(mikeConsultantAtMicrosoft);
      microsoftEmployees.add(passeroDirectorAtMicrosoft);
      microsoft.setEmployees(microsoftEmployees);
      s.save(microsoft);
      
      Company redHat = new Company("Red Hat");
      Employee mikeDeveloperAtRedHat = new Employee(mike, developer);
      Employee passeroConsultantAtRedHat= new Employee(passero, consultant);
      Set<Employee> redHatEmployees = new HashSet<Employee>();
      redHatEmployees.add(mikeDeveloperAtRedHat);
      redHatEmployees.add(passeroConsultantAtRedHat);
      redHat.setEmployees(redHatEmployees);
      s.save(redHat);
      
      s.getTransaction().commit();
      s.close();
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 4:44 am 
Newbie

Joined: Wed Aug 01, 2007 4:53 pm
Posts: 11
That doesn't do the trick i guess...

The think is, people can add function by them self so they can add a person to a company and assign a new function for example add Person A to company A with a function of project manager, add person B to company A with a function of director, person C as regular employee so i want to have a set of persons from one company with there function. Normally a person can only have one function in one company but the same person can have another function in another company.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 5:20 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
In my test code I add myself to Microsoft as a consultant and myself to red hat as a developer - isn't this what you want? If functions can be assigned later you could create a concrete function of "unassigned" or have the function column of Employee set to not-null="true".


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