10:19 am - Tuesday January 22, 2019

Hibernate Association Mapping Using One-To-Many to Fetching 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.util.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.Transaction;

public class MainClass {

	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 List name(String[] args) throws Exception {
		List<Student> list = null;
		try {
			DataInputStream ds = new DataInputStream(System.in);
			System.out.println("*********************");
			System.out.println("Enter the Name to Search");
			String nam = ds.readLine();

			Session s = getSession();
			Query query = s
					.createQuery("from Student s where s.studentName =:nam");
			query.setString("nam", nam);
			list = query.list();

		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}

}

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

}

SearchByName.java:

package com.candidjava;

import java.util.*;

public class SearchByName {
	public static void main(String args[]) {
		try {
			MainClass ctrl = new MainClass();
			List<Student> li = ctrl.name(args);

			System.out.println("******************************************");
			System.out.println("name" + "\t\t" + "PhoneType" + "\t"
					+ "Phone Number");
			System.out.println("*******************************************");

			for (Student s : li) {
				System.out.print("" + s.getStudentName());
				Set<Phone> ph = new HashSet<Phone>();
				ph = s.getStudentPhoneNumbers();
				for (Phone p : ph) {
					System.out.print("\t\t" + p.getPhoneType() + "\t\t" + ""
							+ p.getPhoneNumber());
					System.out.println();
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println("hello");
		}
	}
}

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">false</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" batch-size="50" table="PHONE_om">
		<id name="phoneId" type="long" column="PID">
			<generator class="increment" />
		</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="STUDENT_om">

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

		<set name="studentPhoneNumbers" table="SPHONE_om" 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>onetotmanyfetch</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