10:21 am - Tuesday January 22, 2019

Hibernate Collection mapping Map 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

Project Structure:

 

 

 

 

 

 

 

 

JAR Files:

 

 

 

 

 

 

 

 

 

 

MainClass.java:

package com.candidjava;

import java.io.DataInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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();

			Map courses = new HashMap();
			courses.put(course1, "courseName");
			courses.put(course, "courseName"); // 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.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

	private long studentId;
	private String studentName;
	private Map courses = new HashMap();

	public Student() {
	}

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

	public Student(String studentName, Map 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 Map getCourses() {
		return this.courses;
	}

	public void setCourses(Map 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" />

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

		</map>
	</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>HibernateMapEx</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

Filed in: Z-A Hibernate codes

Comments are closed.