11:30 am - Tuesday January 22, 2019

Hibernate collection mapping List 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

List:

List can contain duplicates.
<list name=”listName” table=”tableName” cascade=”all”>
<key column=”anyColumnName”></key>
<list-index column=”anyColumnName”>
</list-index>
<element column=”anyColumnName” type=”string” />
</list>

List name — which is same as list name in java class.

List-index column — any column name which stores the index of the element, index position start from 0.

element column – any column name which stores the value of the index position given above.

Project Structure:

 

 

 

 

 

 

 

 

JAR Files:

 

 

 

 

 

 

 

 

 

 

 

MainClass.java:

package com.candidjava;

import java.io.DataInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
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("mathan");
			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 Courses");
			String course = d.readLine();
			String course1 = d.readLine();
			System.out.println("Enter Student Name");
			String name = d.readLine();
			String name1 = d.readLine();

			List<String> courses = new ArrayList<String>();
			courses.add(course);
			courses.add(course1); // courses consist of struts and ejb

			Student student1 = new Student(name, courses); // more than one
			// course...many-to-one
			Student student2 = new Student(name1, courses);
			s.save(student2);
			s.save(student1);
			tx.commit();

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

	}

}

Student.java:

package com.candidjava;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

	private long studentId;
	private String studentName;
	private List<String> courses = new ArrayList<String>(0);

	public Student() {
	}

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

	public Student(String studentName, List<String> courses) {
		this.studentName = studentName;
		this.courses = courses;
	}

	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<String> getCourses() {
		return this.courses;
	}

	public void setCourses(List<String> courses) {
		this.courses = courses;
	}

}

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">create</property>
		<mapping resource="\com\\xml\\Student.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="STUDENT">

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

		<list name="courses" table="SCOURSE" cascade="all">
			<key column="SID"></key>
			<list-index column="title">
			</list-index>
			<element column="cid" type="string" />

		</list>
	</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>HibernateListEx</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

Comments are closed.