-->
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: how to do Composition in unidirectinal one-one
PostPosted: Thu Jun 12, 2008 3:20 pm 
Newbie

Joined: Thu Jun 12, 2008 3:06 pm
Posts: 1
Hi

i have two tables named

Employee Address

empId empId
empName city
sal street

and i have two java classes named Employee and Address

public class Employee {

private long empId;

private String empName;

private int sal;

private Address address;

// getters and setters

}


public class Address {

private String empId;

private String city;

private String street;

// getters and setters
}

and .hbm.xml

<?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="com.Employee" table="MyEmp1" >
<id name="empId" column="empId">
<generator class="identity"/>
</id>

<property name="empName" column="empName"/>

<property name="sal"/>

<one-to-one name="address" class="com.Address" cascade="all"/>

</class>

<class name="com.Address" table="address">
<id name="empId" column="empId">
<generator class="foreign">
<param name="property">employee</param>
</generator>
</id>
<property name="city" column="city"/>
<property name="address" column="address"/>
<one-to-one name="employee" class="com.Employee" constrained="true"/>
</class>

</hibernate-mapping>


and it is not working for me... how to do it ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 4:46 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
So, you have a one to one with a User and an Address, or a one to many?

I have a little tutorial that maps a one-to-one relationship between an Exam and an ExamDetail. It's a one-to-one.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=17mappingonetooneassociations

Image

I use annotations, but you just have to find the equivalent xml mapping. With annotations it looks like this:

Code:
@Entity
@Table(name = "exam", schema = "examscam")
public class Exam {
/* @Basic annotation has been
added to all basic fields*/

  private int id;
  private String shortName;

  private ExamDetail detail;

  @Id
  @GeneratedValue
  @Column(name = "id")
  public int getId() { return id; }
  private void setId(int id) {this.id = id;}

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
  @JoinColumn(name="detail_id")
  public ExamDetail getDetail() {return detail; }
  public void setDetail(ExamDetail detail) {
    this.detail = detail;
  }   

  @Basic
  public String getShortName() { return shortName;}
  public void setShortName(String shortName) {
    this.shortName = shortName;
  }
}



Now the detail class:

Code:
@Entity
@Table(name = "exam_detail", schema = "examscam")
public class ExamDetail {
/* @Basic annotation has been
added to all basic fields*/
  private int id;
  private String fullName;
  private int numberOfQuestions;
  private int passingPercentage; 
  private Exam exam;
 
  @Id
  @GeneratedValue
  @Column(name = "id")
  public int getId() {return id;}
  private void setId(int id) {this.id = id;}

  @OneToOne(cascade=CascadeType.ALL, mappedBy="detail")
  public Exam getExam(){return exam;}
  public void setExam(Exam exam){this.exam=exam;}

  @Basic
  public String getFullName() {return fullName;}
  public void setFullName(String fullName) {
    this.fullName = fullName;
  }

  @Basic
  public int getNumberOfQuestions() {
   return numberOfQuestions;
  }
public void setNumberOfQuestions(int numberOfQuestions){
    this.numberOfQuestions = numberOfQuestions;
  }

  @Basic
  public int getPassingPercentage() {
   return passingPercentage;
  }
  public void setPassingPercentage(int passingPercentage){
    this.passingPercentage = passingPercentage;
  }


Once that's done, running the code is very natural:

Code:
public static void main(String args[]){
  HibernateUtil.recreateDatabase();
  Exam exam = new Exam();
  exam.setShortName("SCJA");

  ExamDetail detail = new ExamDetail();
  detail.setFullName("Sun Certified Java Associate");
  detail.setPassingPercentage(62);
  detail.setNumberOfQuestions(55);
  exam.setDetail(detail);

  Session session = HibernateUtil.beginTransaction();
  //possible due to CascadeType.ALL
  session.save(exam);
  HibernateUtil.commitTransaction();
}


Here's the tutorial if you want to follow through it.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=17mappingonetooneassociations

You say its not working. Is there a specific exception you are getting that we can use to narrow down the problem?[/url]

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.