10:17 am - Tuesday January 22, 2019

Hibernate Association Mapping using Many-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:

 

 

 

 

 

 

 

 

 

 

Course.java:

package com.candidjava;

/**
 * This class contains the course details.
 *
 */
public class Course implements java.io.Serializable {

	private long courseId;
	private String courseName;

	public Course() {
	}

	public Course(String courseName) {
		this.courseName = courseName;
	}

	public long getCourseId() {
		return this.courseId;
	}

	public void setCourseId(long courseId) {
		this.courseId = courseId;
	}

	public String getCourseName() {
		return this.courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

}

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("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();
			Set<Course> courses = new HashSet<Course>();
			courses.add(new Course(course));
			courses.add(new Course(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); // sam is having two
			// courses like wise
			// struts is having
			// two students
			s.save(student1);
			s.save(student2);

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

	}

}

Student.java:

package com.candidjava;

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

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

	private long studentId;
	private String studentName;
	private Set<Course> courses = new HashSet<Course>(0);

	public Student() {
	}

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

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

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}

}

Course.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.Course" table="COURSE">
		<id name="courseId" type="long" column="CID">
			<generator class="native" />
		</id>
		<property name="courseName" type="string" column="CNAME" />
	</class>

</hibernate-mapping>

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\\Course.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="STUDENT3">

		<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="courses" table="SCOURSE" cascade="all">
			<key column="SID" />
			<many-to-many column="CID" class="com.candidjava.Course" />
		</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>many-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