Friday, 21 June 2013

ALab5 : Collection Mapping using Hibernate Annotation

Steps in MyEclipse :
1. Create a Java project with name : ALab5
2. Add JARs same as ALab2
3. Write all files as ALab2 (not same)

So, all files needed are:
1. hibernate.cfg.xml
2. AHibernateUtil.java
3. Student.java
4. Lab5Client.java

You will get a directory structure as the following image :

















Codes Of ALab5:
1. hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3333/practice</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.annotation.persistence.Student"/>
</session-factory>
</hibernate-configuration>

2. AHibernateUtil.java
package com.annotation.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class AHibernateUtil {
static SessionFactory factory;
static{
try{
AnnotationConfiguration acfg = new AnnotationConfiguration();
acfg = (AnnotationConfiguration)acfg.configure();
factory = acfg.buildSessionFactory();
}catch (Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSF(){
return factory;
}
}

3. Student.java
package com.annotation.persistence;
import java.util.*;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.*;
import org.hibernate.annotations.MapKey;
@Entity
@Table(name="students")
public class Student {
@Id
@GenericGenerator(name="gg",strategy="increment")
@GeneratedValue(generator="gg")
@Column(name="sid")
private int sid;
@Column(name="sname")
private String sname;
@CollectionOfElements
@JoinTable(name="courses",joinColumns=@JoinColumn(name="sid"))
@IndexColumn(name="idx")
@Column(name="course")
private String[] courses;
@CollectionOfElements
@JoinTable(name="emails",joinColumns=@JoinColumn(name="sid"))
@IndexColumn(name="idx")
@Column(name="emailId")
private List<String> emails;
@CollectionOfElements
@JoinTable(name="marks",joinColumns=@JoinColumn(name="sid"))
@IndexColumn(name="idx")
@Column(name="marks")
private List<Integer> marks;
@CollectionOfElements
@JoinTable(name="phones",joinColumns=@JoinColumn(name="sid"))
@Column(name="phoneNo")
private Set<Long> phones;
@CollectionOfElements
@JoinTable(name="refs",joinColumns=@JoinColumn(name="sid"))
@MapKey(columns={@Column(name="sname")})
@Column(name="sphone")
private Map<String,Long> refs;
public Student(String sname, String[] courses, List<String> emails,
List<Integer> marks, Set<Long> phones, Map<String, Long> refs) {
super();
this.sname = sname;
this.courses = courses;
this.emails = emails;
this.marks = marks;
this.phones = phones;
this.refs = refs;
}
public int getSid() {
return sid;
}
public String getSname() {
return sname;
}
public String[] getCourses() {
return courses;
}
public List<String> getEmails() {
return emails;
}
public List<Integer> getMarks() {
return marks;
}
public Set<Long> getPhones() {
return phones;
}
public Map<String, Long> getRefs() {
return refs;
}
public void setSid(int sid) {
this.sid = sid;
}
public void setSname(String sname) {
this.sname = sname;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public void setMarks(List<Integer> marks) {
this.marks = marks;
}
public void setPhones(Set<Long> phones) {
this.phones = phones;
}
public void setRefs(Map<String, Long> refs) {
this.refs = refs;
}
}


4. Lab5Client.java
package com.annotation.persistence;
import java.util.ArrayList;
import java.util.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.annotation.util.AHibernateUtil;
public class Lab5Client {
public static void main(String[] args){
Transaction tx = null;
try{
SessionFactory sf = AHibernateUtil.getSF();
Session session = sf.openSession();
tx = session.beginTransaction();
String[] courses = {"Java","JSP","EJB"};
List<String> emails = new ArrayList<String>();
emails.add("abc");
emails.add("def");
emails.add("ghi");
List<Integer> marks = new ArrayList<Integer>();
marks.add(new Integer(100));
marks.add(new Integer(99));
marks.add(new Integer(89));
Set<Long> phones = new HashSet<Long>();
phones.add(new Long(1122));
phones.add(new Long(2266));
phones.add(new Long(4488));
Map<String,Long> refs = new HashMap<String,Long>();
refs.put("a", new Long(1122));
refs.put("b", new Long(2244));
refs.put("c", new Long(5858));
Student stud = new Student("sonu", courses, emails, marks, phones, refs);
Integer in = (Integer)session.save(stud);
int inti = in.intValue();
System.out.println(inti);
tx.commit();
}catch (Exception e) {
e.printStackTrace();
if(tx!=null)
tx.rollback();
}
}
}
5. Tables
same as CLab4

* Run as Java Application.
All tables will be updated as :



No comments:

Post a Comment