• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how to use one-to-one association

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am trying to develop a hibernate application using one-to-one association. I have two POJOs named Employee and Message, I have a table employee. The employee table have the empname, empid, salary, company, email, message as the columns. My question is If I set the value in the Message POJO, that value has to be inserted into the database. I got the output what I expected with the many-to-one tag. But now I want to get the output with one-to-one association. Can anybody pls help me how to do that ??
**** I want to insert the data into a single table using two POJOs ****
Here is my code :
Employee POJO :

package events;

public class Employee {

private String empname;
private int empid;
private int salary;
private String company;
private String email;
private Message message;

public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public Employee() {
}
}

Message POJO :

package events;

public class Message extends Employee{

private String msg;

public String getMsg() {
return msg;
}
public void setMsg(String message) {
this.msg = message;
}
public Message() {
}
}

Employee.hbm.xml :

<?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>
<class name="events.Employee" table="employee">
<id name="empname" column="empname"></id>
<property name="empid" column="empid"></property>
<property name="salary" column="salary"></property>
<property name="company" column="company"></property>
<property name="email" column="email"></property>
<many-to-one name="message" class="events.Message" column="message" cascade="all"/>
</class>
</hibernate-mapping>


Can anybody pls tell me how to get the output by using <one-to-one/> ???
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this: http://www.hibernate.org/hib_docs/reference/en/html/associations.html
 
Honk if you love justice! And honk twice for tiny ads!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic