3:14 am - Friday April 19, 2019

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

Array:

<array name=”arrayName” table=”tableName” cascade=”all”>
<key column=”anyColumnName” />
<list-index column=”anyColumnName” base=”1″>
</list-index>
<many-to-many column=”anyColumnName” unique=”true” class=”className” />
</array>

array name – name matches with array name in java.

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

list-index column – any column name which is used to map the tables.

Base=”1” – Its optional. It’s default value is zero. List starts from one so, here base=”1”.

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

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 Object[] name(String[] args) throws Exception {
		List list = null;
		Object[] list1 = 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();
			list1 = list.toArray();
		} catch (HibernateException e) {
			System.out.println("ddd" + e.getMessage());
		}
		return list1;
	}
}

Phone.java:

package com.candidjava;

public class Phone implements java.io.Serializable {

	private int phoneId;
	private String phoneType;
	private String phoneNumber;

	Phone() {

	}

	public Phone(String phoneType, String phoneNumber) {
		this.phoneType = phoneType;
		this.phoneNumber = phoneNumber;
	}

	public int getPhoneId() {
		return this.phoneId;
	}

	public void setPhoneId(int 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();
			Object[] li = ctrl.name(args);
			for (Object s2 : li) {
				Student s = (Student) s2;
				System.out.print("" + s.getStudentName());
				Phone[] p = s.getStudentPhoneNumbers();

				for (Phone ob : p) {
					System.out.println();
					System.out.println("Phone type " + ob.getPhoneType());
					System.out.println("Phone Number" + ob.getPhoneNumber());

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

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 Phone[] studentPhoneNumbers;

	// private List studentPhoneNumbers = (List) new
	// HashSet(0);

	Student() {

	}

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

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

	public void setStudentPhoneNumbers(Phone[] studentPhoneNumbers) {
		this.studentPhoneNumbers = studentPhoneNumbers;
	}

}

Web.xml:

<!--?xml version="1.0" encoding="UTF-8"?-->

	HibernateArrayEx

		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp

hibernate.cfg.xml:


			oracle.jdbc.OracleDriver

			jdbc:oracle:thin:@localhost:1521:XE

			system

			system

		5
		<!-- SQL dialect -->

			org.hibernate.dialect.OracleDialect

		<!-- Echo all executed SQL to stdout -->
		true
		update

Phone.hbm.xml:

<!--?xml version="1.0"?-->

Student.hbm.xml:

<!--?xml version="1.0"?-->

OUTPUT:




 

DOWNLOAD+SOURCECODE                                     

BACK

Filed in: Z-A All Java Codes

Comments are closed.