10:29 am - Tuesday January 22, 2019

Hibernate Association Mapping Using One-To-Many 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:

 

 

 

 

 

 

 

 

 

 

MainClass.java:

package com.candidjava;

import java.io.DataInputStream;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainClass {

	private static SessionFactory sessionFactory;

	public static void main(String[] args) {

		try {
			sessionFactory = new Configuration().configure(
					"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
		} catch (Exception e)

		{

			System.out.println(e.getMessage());
			System.err.println("Initial SessionFactory creation failed." + e);

		}
		try {
			Session s = sessionFactory.openSession();
			Transaction tx = s.beginTransaction();
			DataInputStream d = new DataInputStream(System.in);
			System.out.println("Enter 2 Phone numbers");
			String phone = d.readLine();
			String phone1 = d.readLine();
			System.out.println("Enter student name");
			String name = d.readLine();

			Set<Phone> phoneNumbers = new HashSet<Phone>();
			phoneNumbers.add(new Phone("house", phone));
			phoneNumbers.add(new Phone("mobile", phone1));

			Student student = new Student(name, phoneNumbers);
			s.save(student);

			tx.commit();
		} catch (Exception e) {
			System.out.println(e);
		}

	}

}

Phone.java:

package com.candidjava;

public class Phone implements java.io.Serializable {

	private long phoneId;
	private String phoneType;
	private String phoneNumber;

	public Phone() {
	}

	public Phone(String phoneType, String phoneNumber) {
		this.phoneType = phoneType;
		this.phoneNumber = phoneNumber;
	}

	public long getPhoneId() {
		return this.phoneId;
	}

	public void setPhoneId(long phoneId) {
		this.phoneId = phoneId;
	}

	public String getPhoneType() {
		return this.phoneType;
	}

	public void setPhoneType(String phoneType) {
		this.phoneType = phoneType;
	}

	public String getPhoneNumber() {
		return this.phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

}

Student.java:

package com.candidjava;

import java.util.HashSet;
import java.util.Set;

/**
 * This class contains student details.
 */
public class Student implements java.io.Serializable {

	private long studentId;
	private String studentName;
	private Set<Phone> studentPhoneNumbers = new HashSet<Phone>(0);

	public Student() {
	}

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

	public Student(String studentName, Set<Phone> studentPhoneNumbers) {
		this.studentName = studentName;
		this.studentPhoneNumbers = studentPhoneNumbers;
	}

	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 Set<Phone> getStudentPhoneNumbers() {
		return this.studentPhoneNumbers;
	}

	public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) {
		this.studentPhoneNumbers = studentPhoneNumbers;
	}

}

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\\Phone.hbm.xml" />

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

Phone.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.Phone" table="PHONE">
		<id name="phoneId" type="long" column="PID">
			<generator class="native" />
		</id>
		<property name="phoneType" type="string" length="10" column="PTYPE" />
		<property name="phoneNumber" type="string" length="15"
			column="PNUMBER" />
	</class>
</hibernate-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.Student" table="STUDENT2">

		<id name="studentId" type="long" column="SID">
			<generator class="native" />
		</id>
		<property name="studentName" type="string" not-null="true"
			length="100" column="SNAME" />

		<set name="studentPhoneNumbers" table="SPHONE" cascade="all">
			<key column="SID" />
			<many-to-many column="PID" unique="true" class="com.candidjava.Phone" />
		</set>
	</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>one-to-many</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