wood burning stoves
The moose likes Object Relational Mapping and the fly likes how to use one-to-one association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to use one-to-one association" Watch "how to use one-to-one association" New topic

how to use one-to-one association

Gayathri Chowduru
Ranch Hand

Joined: Oct 17, 2007
Posts: 42
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"

<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"/>

Can anybody pls tell me how to get the output by using <one-to-one/> ???
Walter Bernstein
Ranch Hand

Joined: Dec 19, 2007
Posts: 57
Check this: http://www.hibernate.org/hib_docs/reference/en/html/associations.html
I agree. Here's the link: http://aspose.com/file-tools
subject: how to use one-to-one association
It's not a secret anymore!