post11:Hibernate One-To-One Mapping Using Annotations - TopicsExpress



          

post11:Hibernate One-To-One Mapping Using Annotations Tutorial Learn how to do one-to-one mapping using hibernate annotations. In this example you will learn how to map one-to-one relationship using Hibernate Annotations. Consider the following relationship between Student and Address entity. According to the relationship each student should have a unique address. To create this relationship you need to have a STUDENT and ADDRESS table. The relational model is shown below. To create the STUDENT and ADDRESS tables you need to create the following Java classes with hibernate annotations. Student class is used to create the STUDENT table. package com.tutorials4u.student; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name = "STUDENT") public class Student { private long studentId; private String studentName; private Address studentAddress; public Student() { } public Student(String studentName, Address studentAddress) { this.studentName = studentName; this.studentAddress = studentAddress; } @Id @GeneratedValue @Column(name = "STUDENT_ID") public long getStudentId() { return this.studentId; } public void setStudentId(long studentId) { this.studentId = studentId; } @Column(name = "STUDENT_NAME", nullable = false, length = 100) public String getStudentName() { return this.studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } @OneToOne(cascade = CascadeType.ALL) public Address getStudentAddress() { return this.studentAddress; } public void setStudentAddress(Address studentAddress) { this.studentAddress = studentAddress; } } The @OneToOne annotation is used to create the one-to-one relationship between the Student and Address entities. Address class is used to create the ADDRESS table. package com.tutorials4u.student; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "ADDRESS") public class Address { private long addressId; private String street; private String city; private String state; private String zipcode; public Address() { } public Address(String street, String city, String state, String zipcode) { this.street = street; this.city = city; this.state = state; this.zipcode = zipcode; } @Id @GeneratedValue @Column(name = "ADDRESS_ID") public long getAddressId() { return this.addressId; } public void setAddressId(long addressId) { this.addressId = addressId; } @Column(name = "ADDRESS_STREET", nullable = false, length=250) public String getStreet() { return this.street; } public void setStreet(String street) { this.street = street; } @Column(name = "ADDRESS_CITY", nullable = false, length=50) public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } @Column(name = "ADDRESS_STATE", nullable = false, length=50) public String getState() { return this.state; } public void setState(String state) { this.state = state; } @Column(name = "ADDRESS_ZIPCODE", nullable = false, length=10) public String getZipcode() { return this.zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } } Now create the hibernate configuration file with the Student and Address class mapping. org.hsqldb.jdbcDriver jdbc:hsqldb:hsql://localhost sa 1 org.hibernate.dialect.HSQLDialect true create Create the Main class to run the example. package com.tutorials4u.student; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.tutorials4u.util.HibernateUtil; public class Main { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Address address1 = new Address("OMR Road", "Chennai", "TN", "600097"); Address address2 = new Address("Ring Road", "Banglore", "Karnataka", "560000"); Student student1 = new Student("Eswar", address1); Student student2 = new Student("Joe", address2); session.save(student1); session.save(student2); transactionmit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } } On executing the Main class you will see the following output. The Student table has two records. The Address table has two record. Each student record points to a different address record, this illustrates the one-to-one mapping. hunk-->java
Posted on: Wed, 14 Aug 2013 17:47:52 +0000

Trending Topics



Recently Viewed Topics




© 2015