10:48 am - Tuesday January 22, 2019

Hibernate Association Mapping Using One-To-One to Inserting Data

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure:

 

 

 

 

 

 

 

 

 

 

 

 

JAR Files:

 

 

 

 

 

 

 

 

 

 

 

CREATE THE FOLLOWING JAVA FILES AND POJO CLASS FILES:

 

 

 

 

Address.java:

package com.candidjava;

public class Address implements java.io.Serializable {

	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;
	}

	public long getAddressId() {
		return this.addressId;
	}

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

	public String getStreet() {
		return this.street;
	}

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

	public String getCity() {
		return this.city;
	}

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

	public String getState() {
		return this.state;
	}

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

	public String getZipcode() {
		return this.zipcode;
	}

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

}

Input.java:

package com.candidjava;

import java.io.*;

public class Input {
	public static void main(String[] arg) {
		try {
			Mapclass M = new Mapclass();
			M.addAddress(arg);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Mapclass.java:

package com.candidjava;

import java.util.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.Transaction;

public class Mapclass {

	private static SessionFactory sessionFactory;

	private Session getSession() {
		Session s = null;
		try {
			sessionFactory = new Configuration().configure(
					"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
			s = sessionFactory.openSession();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return s;
	}

	public void addAddress(String[] arg) throws Exception {
		try {
			Session s = getSession();
			Transaction transaction = s.beginTransaction();
			DataInputStream ds = new DataInputStream(System.in);
			System.out.println("****************************");
			System.out.println("Enter the Address1");
			System.out.println("Enter the Street");
			String street1 = ds.readLine();
			System.out.println("Enter the city");
			String city1 = ds.readLine();
			System.out.println("Enter the state");
			String state1 = ds.readLine();
			System.out.println("Enter the zip");
			String zip1 = ds.readLine();

			Address address1 = new Address(street1, city1, state1, zip1);
			// By using cascade=all option the address need not be saved
			// explicitly when the student object is persisted the address will
			// be automatically saved. session.save(address);
			// s.save(address);
			System.out.println("****************************");
			System.out.println("Enter the Address2");
			System.out.println("Enter the Street");
			String street2 = ds.readLine();
			System.out.println("Enter the city");
			String city2 = ds.readLine();
			System.out.println("Enter the state");
			String state2 = ds.readLine();
			System.out.println("Enter the zip");
			String zip2 = ds.readLine();
			Address addres2 = new Address(street2, city2, state2, zip2);

			System.out.println("Enter the First student name");
			String sname1 = ds.readLine();
			System.out.println("Enter the second student name");
			String sname2 = ds.readLine();

			Student student1 = new Student(sname1, address1);

			Student student2 = new Student(sname2, addres2);

			s.save(student1);
			s.save(student2);
			System.out.println("Data saved");
			transaction.commit();
		} catch (HibernateException e) {

			e.printStackTrace();
		}

	}
}

Student.java:

package com.candidjava;

public class Student implements java.io.Serializable {

	private long studentId;
	private String studentName;
	private Address studentAddress;// studentaddress is

	// studentclass(associating) property but it
	// is of type addressclass(associated class)

	public Student() {
	}

	public Student(String studentName, Address studentAddress) {
		this.studentName = studentName;
		this.studentAddress = studentAddress;
	}

	public long getStudentId() {
		return this.studentId;
	}

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

	public String getStudentName() {
		return this.studentName;
	}

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

	public Address getStudentAddress() {
		return this.studentAddress;
	}

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

}

CREATE THE FOLLWING XML FILES :

 

 

 

Address.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.Address" table="ADDR">
		<id name="addressId" type="long" column="aid">
			<generator class="native" />
		</id>
		<property name="street" column="street" type="string" />
		<property name="city" column="city" type="string" />
		<property name="state" column="state" type="string" />
		<property name="zipcode" column="zip" type="string" />
	</class>
</hibernate-mapping>

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="studentFactory">
		<property name="connection.driver_class">
			oracle.jdbc.OracleDriver
	        </property>
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:XE
		</property>
		<property name="connection.username">
			system
		</property>
		<property name="connection.password">
			system
		</property>
		<property name="connection.pool_size">5</property>
		<!-- SQL dialect -->
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
	        </property>
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<mapping resource="\com\\xml\\Student.hbm.xml" />
		<mapping resource="\com\\xml\\Address.hbm.xml" />

	</session-factory>
</hibernate-configuration>

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.Student" table="STUD2">

		<id name="studentId" type="long" column="ID">
			<generator class="native" />
		</id>
		<property name="studentName" type="string" length="100"
			not-null="true" column="name" />
		<many-to-one name="studentAddress" class="com.candidjava.Address"
			column="STUDENT_ADDRESS" cascade="all" not-null="true" unique="true" />
	</class>
</hibernate-mapping>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>OneToOne</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

OUTPUT:

 

DOWNLOAD+SOURCECODE 

BACK  

Filed in: Z-A Hibernate codes

No comments yet.

Leave a Reply