Hibernate Component mapping with dependent object xml based


Component dependent object

A component is a contained object that is persisted as a value type and not an entity reference. The term "component" refers to the object-oriented notion of composition and not to architecture-level components. 


Component xml mapping

Student.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.candidjava.hibernate.Student" table="Student_component">

		<id name="studentId" type="long" column="STUDENT_ID">
			<generator class="increment" />
		</id>
		<property name="studentName" type="string" not-null="true"
			column="STUDENT_NAME" />

		<component name="studentAddress" class="com.candidjava.hibernate.Address">
			<property name="street" column="ADDRESS_STREET" type="string"
				length="250" />
			<property name="city" column="ADDRESS_CITY" type="string"
				length="50" />
			<property name="state" column="ADDRESS_STATE" type="string"
				length="50" />
			<property name="zipcode" column="ADDRESS_ZIPCODE" type="string"
				length="10" />
		</component>

	</class>
</hibernate-mapping>

Pojo classes

Student.java
package com.candidjava.hibernate;

public class Student implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	private long studentId;
	private String studentName;
	private Address studentAddress;

	public long getStudentId() {
		return studentId;
	}

	public void setStudentId(long studentId) {
		this.studentId = studentId;
	}

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public Address getStudentAddress() {
		return studentAddress;
	}

	public void setStudentAddress(Address studentAddress) {
		this.studentAddress = studentAddress;
	}

}
Address.java
package com.candidjava.hibernate;

public class Address implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	private long addressId;
	private String street;
	private String city;
	private String state;
	private String zipcode;

	public long getAddressId() {
		return addressId;
	}

	public void setAddressId(long addressId) {
		this.addressId = addressId;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public Address() {

	}

}

Saving Objects to Component mapping
	public void insertStudent(Student student) {
		Session s = getSession();
		Transaction transaction = s.beginTransaction();
		s.save(student);
		transaction.commit();
	}

Retrieving Objects from Component Mapping (session.get())
public Student getStudent(long id) {
		Student sd = null;
		Session s = getSession();
		sd = (Student) s.get(Student.class, id);
		return sd;
}

Download Example
Component Mapping war
Component Mapping Zip




Related Post

Comments


©candidjava.com