-->
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: one to one mapping
PostPosted: Thu Dec 24, 2009 12:06 am 
Newbie

Joined: Thu Nov 12, 2009 4:32 pm
Posts: 16
i am newbie to hibernate, Here are the two objects

1)
Code:
       public class Employee implements Serializable {

        private Long userId;
        private Long supportId;
   private Long typeId;
   private String firstName;
   private String lastName;
   private String emailAddress;
   private String phoneNumber;
        private String userFlag;
        private String companyId;
   private SupportBean supportBean;

   public Employee() {
      super();
      setUserFlag("Y");
      setTypeId(new Long(6));
   }
   public String getEmailAddress() {
      return emailAddress;
   }
   public void setEmailAddress(String emailAddress) {
      this.emailAddress = emailAddress;
   }
   public String getFlag() {
      return flag;
   }
   public void setFlag(String flag) {
      this.flag = flag;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public Long getSupportId() {
      return supportId;
   }
   public void setSupportId(Long supportId) {
      this.supportId = supportId;
   }
   public Long getTypeId() {
      return typeId;
   }
   public void setTypeId(Long typeId) {
      this.typeId = typeId;
   }
   public Long getCompanyIdId() {
      return companyId;
   }
   public void setCompanyId(Long companyId) {
      this.companyId = companyId;
   }
   public String getPhoneNumber() {
      return phoneNumber;
   }
   public void setPhoneNumber(String phoneNumber) {
      this.phoneNumber = phoneNumber;
   }
   public SupportBean getSupportBean() {
      return supportBean;
   }

       public void setSupportBean(SupportBean supportBean) {
      this.supportBean = supportBean;
   }
}


2)
Code:
public class SupportBean implements Serializable {

        private Long supportId;
   private String name;
   private String status;
        private Employee employee;

   public SupportBean() {
      super();
      
   }
   public Long getSupportId() {
      return supportId;
   }
   public void setSupportId(Long supportId) {
      this.supportId = supportId;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }

        public String getStatus() {
      return status;
   }
       public void setStatus(String status) {
      this.status = status;
   }
   public Employee getEmployee() {
      return employee;
   }
   public void setEmployee(Employee employee) {
      this.employee = employee;
   }
}


3) Employee.hbm.xml
Code:
<hibernate-mapping package="com.emc.servicecenter.user">
  <class name="Employee" table="table_employee">
  [b] <id name="userId" column="user_id"/>[/b]
   [color=#BF0040]<property name="supportId" column="support_id"></property>[/color]
   <property name="typeId" column="type_id"></property>
   <property name="firstName" column="first_name"></property>
   <property name="lastName" column="last_name"></property>
   <property name="emailAddress" column="email_address"></property>
   <property name="phoneAddress" column="phone_address"></property>
   <property name="userFlag" column="user_flag"></property>
  [color=#4000FF] <property name="companyId" column="company_id"></property>   [/color]

  [b] <one-to-one name="supportBean" class="SupportBean" constrained="true"/>[/b]
</class>
</hibernate-mapping>


4)
Code:
<hibernate-mapping package="com.emc.servicecenter.user">
  <class name="SupportBean" table="table_support">
  [b]  <id name="supportId" column="support_id"></id>[/b]
    <property name="name" column="name"></property>
    <property name="status" column="status"></property>
   [b] <one-to-one name="employee" class="Employee"/>[/b]
  </class>
</hibernate-mapping>


Here is how both are related

1) Employee has one-to-one with SupportBean object
2) userId is the primary key of Employee object
3) supportId is the Foreign Key of Employee Object
3) supportId is the PK of SupportBean object

so i have a small test like below

SessionFactory sessionFactory = new Configuration().configure("xxx.cfg.xml").buildSessionFactory();
session = sessionFactory.openSession();
Employee emp = (Employee)session.get(Employee.class,new Long(1000));
System.out.println("output=" + emp.getFirstName() + ":" + emp.getSupportBean());

results: Jack : null

am i doing anything wrong with the mapping, could some one shed light on this please

thanks
DP


Top
 Profile  
 
 Post subject: Re: one to one mapping
PostPosted: Mon Dec 28, 2009 6:09 am 
Newbie

Joined: Wed Dec 23, 2009 12:38 pm
Posts: 14
Your Employee class has foreign key and hence should have mapping element as many-to-one with unique=true , instead of one-to-one.


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.