10:34 am - Tuesday January 22, 2019

Hibernate Collection mapping bag Using Eclipse

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

Bag:

Bag allows duplicates.
<bag name=”studentPhoneNumbers” table=”SPHONE_om” cascade=”all”>
<key column=”SID” />
<many-to-many column=”PID” unique=”true” class=”className” />
</bag>

bag name  — Matches the list name in java class.

Key column – any unique column name for the given table.

Many-to-many column – Column name which is used to map the table with other table.

It accepts list in java class.

(Bag is same as list; it is used to counts number of times the element appears).

ProjectStructure:                                    

 

 

 

 

 

 

 

 

 

 JarFiles:

 

 

 

 

 

 

 

 

 

 

 

 

 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("ddd" + 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;

	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);
			for (Student s : li) {
				System.out.print("" + s.getStudentName());
				List<Phone> ph = new ArrayList<Phone>();
				ph = s.getStudentPhoneNumbers();
				for (Phone p : ph) {
					System.out.println();
					System.out.println("Phone type " + p.getPhoneType());
					System.out.println("Phone Number" + 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.List;
import java.util.Set;

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

	private long studentId;
	private String studentName;
	private List<Phone> studentPhoneNumbers;

	// private List<Phone> studentPhoneNumbers = (List<Phone>) new
	// HashSet<Phone>(0);

	Student() {

	}

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

	public Student(String studentName, List<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 List<Phone> getStudentPhoneNumbers() {
		return studentPhoneNumbers;
	}

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

}

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>HibernatreBagEx</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>

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" 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" />

		<bag name="studentPhoneNumbers" table="SPHONE_om" cascade="all">
			<key column="SID" />
			<many-to-many column="PID" unique="true" class="com.candidjava.Phone" />
		</bag>
	</class>
</hibernate-mapping>

 OUTPUT: 

 DOWNLOAD+SOURCECODE 

BACK

                        

                

Filed in: Z-A Hibernate codes

Comments are closed.