Collection Data Types
XML Based Mapping
public class Student {
private int id;
private String name;
private int[] mobileNumbers;
private String[] projects;
private Collection<String> skills;
private List<Integer> marks;
private Set<String> jobs;
private Map<String, String> educations;
//sertters......getters
}
HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){}
static {
StandardServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metaData =
new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
public class Student {
private int id;
private String name;
private int[] mobileNumbers;
private String[] projects;
private Collection<String> skills;
private List<Integer> marks;
private Set<String> jobs;
private Map<String, String> educations;
//sertters......getters
}
HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){}
static {
StandardServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metaData =
new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
student.hbm.xml
<hibernate-mapping
package="com.practice.colection.types.xml" auto-import="false">
<class name="Student" table="STUDENT_COLLECTION1">
<id name="id" column="S_ID" />
<property name="name" />
<primitive-array name="mobileNumbers"
table="MOBILE_NUMBERS1">
<key column="S_ID" />
<list-index column="sno" />
<element column="m_no" type="int" />
</primitive-array>
<array name="projects" table="PROJECTS1">
<key column="S_ID" />
<list-index column="sno" />
<element column="project_name" type="string" />
</array>
<bag name="skills" table="SKILLS1" lazy="true">
<key column="S_ID" />
<element column="skill_name" type="string"></element>
</bag>
<list name="marks" table="MARKS1">
<key column="S_ID" />
<list-index column="sno" />
<element column="mark" type="int" />
</list>
<set name="jobs" table="JOBS1">
<key column="S_ID" />
<element column="job" type="string" />
</set>
<map name="educations" table="EDUCATIONS1">
<key column="S_ID" />
<map-key column="name" type="string" />
<element column="education" type="string" />
</map>
</class>
</hibernate-mapping>
Annotation Based Mapping
Student.java
@Entity(name="com.practice.colection.types.annotation.Student")
@Table(name="STUDENT_COLLECTION2")
public class Student {
@Id
private int id;
private String name;
@ElementCollection
@CollectionTable(name="MOBILE_NUMBERS2",
joinColumns= @JoinColumn(name="S_ID"))
@OrderColumn(name="sno")
@ListIndexBase(10)//start index with 10, 11 ...
@Column(name="m_number")
private int[] mobileNumbers;
@ElementCollection
@CollectionTable(name="PROJECTS2",
joinColumns = @JoinColumn(name="S_ID"))
@OrderColumn(name="sno")
@Column(name="project_name")
private String[] projects;
@ElementCollection
@CollectionTable(name="SKILLS2",
joinColumns = @JoinColumn(name="S_ID"))
@Column(name="skill_name")
private Collection<String> skills;
@ElementCollection
@CollectionTable(name="MARKS2",
joinColumns= @JoinColumn(name="S_ID"))
@OrderColumn(name="sno")
@Column(name="mark")
private List<Integer> marks;
@ElementCollection
@CollectionTable(name="JOBS2",
joinColumns= @JoinColumn(name="S_ID"))
@Column(name="job")
private Set<String> jobs;
@ElementCollection
@CollectionTable(name="EDUCATIONS2",
joinColumns= @JoinColumn(name="S_ID"))
@MapKeyColumn(name="education_type")
@Column(name="education_name")
private Map<String, String> educations;
//setters........getters.........
}
Test Class Manager1.java
public class Manager1 {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();
Student student = new Student();
student.setId(101);
student.setName("Raghvendra");
student.setMobileNumbers(new int[]{1234567890, 456843256, 456234598});
String[] projects = {"citi", "hdfc", "kotak"};
student.setProjects(projects);
Collection skills = new ArrayList();
skills.add("C");
skills.add("C++");
skills.add("Java");
student.setSkills(skills);
List marks = new ArrayList();
marks.add(100);
marks.add(200);
marks.add(300);
student.setMarks(marks);
Set jobs = new HashSet();
jobs.add("Software Engineer");
jobs.add("Manager");
jobs.add("Vice President");
student.setJobs(jobs);
Map educations = new HashMap();
educations.put("degree", "BSc");
educations.put("marters", "Msc");
student.setEducations(educations);
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
System.out.println("Records inserted in DB");
}
}
Comments
Post a Comment