Persisting collection of objects

In this example we will see how to store a collection of objects. If we have a set of address objects, then they can be stored in the data base as shown in the below example. The output of collection objects is generated in a separate table.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">hr</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.ram.dao.UserDetails"/>
        <mapping class="com.ram.dao.Address"/>


    </session-factory>

</hibernate-configuration>
package com.ram.dao;

import javax.persistence.Embeddable;

@Embeddable
public class Address {
 private String street;
 private String city;
 private String state;
 
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
}

package com.ram.dao;

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

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class UserDetails {
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 private int userId;
 private String userName;
 @ElementCollection
 private Set
listOfAddresses = new HashSet(); public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Set
getListOfAddresses() { return listOfAddresses; } public void setListOfAddresses(Set
listOfAddresses) { this.listOfAddresses = listOfAddresses; } }
package com.ram.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.ram.dao.Address;
import com.ram.dao.UserDetails;

public class CollectionObjectsExample {
 public static void main(String[] args) {
  UserDetails userDetails = new UserDetails();
  userDetails.setUserName("Ram");
  
  Address address1 = new Address();
  address1.setStreet("Richmond");
  address1.setCity("Melbourne");
  address1.setState("Victoria");
  
  Address address2 = new Address();
  address2.setStreet("Parramatta");
  address2.setCity("Sydney");
  address2.setState("NSW");
  
  userDetails.getListOfAddresses().add(address1);
  userDetails.getListOfAddresses().add(address2);
  
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  
  session.beginTransaction();
  session.save(userDetails);
  session.getTransaction().commit();
  
  session.close();
 }
}

Execute the CollectionObjectsExample.java and you get the below output.

No comments:

Post a Comment