This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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"
"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/> ???
Walter Bernstein
Ranch Hand

Joined: Dec 19, 2007
Posts: 57
Check this: http://www.hibernate.org/hib_docs/reference/en/html/associations.html
 
 
subject: how to use one-to-one association
 
Similar Threads
Hibernate association mapping
Hibernate exception for one-to-one mapping
Hibernate Association
HBM mapping Problem
Association mapping