@ElementCollection:
It is used to save collection of elements
HibernateTest.java
package
org.yash.hibernate;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.Configuration;
import org.yash.dto.Address;
import
org.yash.dto.UserDetails;
public class HibernateTest {
public static void main(String
args[]){
UserDetails
user = new UserDetails();
user.setUserName("First
User");
UserDetails
user2 = new UserDetails();
user2.setUserName("Second
User");
Address addr1 = new Address();
addr1.setCity("First
City");
addr1.setPincode("9999");
addr1.setState("AB");
addr1.setStreet("2355
SE Saint");
Address
addr2 = new Address();
addr2.setCity("Second
City");
addr2.setPincode("7676");
addr2.setState("AC");
addr2.setStreet("2399 Hilton");
user.getListOfAddresses().add(addr1);
user.getListOfAddresses().add(addr2);
SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
Session
session = sessionFactory.openSession();
/* It is used
to save all the objects and to define single unit of work */
session.beginTransaction();
session.save(user);
//session.save(user2);
session.getTransaction().commit();
user
= null;
session
= sessionFactory.openSession();
session.beginTransaction();
user
= (UserDetails) session.get(UserDetails.class, 1);
System.out.println("User
name retrieved is "+user.getUserName());
}
}
Address.java
package org.yash.dto;
import
javax.persistence.Column;
import
javax.persistence.Embeddable;
@Embeddable
public class Address {
@Column(name="STREET_NAME")
private String street;
@Column(name="CITY_NAME")
private String city;
@Column(name="STATE_NAME")
private String state;
@Column(name="PIN_CODE")
private String pincode;
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;
}
public String
getPincode() {
return pincode;
}
public void
setPincode(String pincode) {
this.pincode = pincode;
}
}
UserDetails.java
package org.yash.dto;
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;
import
javax.persistence.Table;
@Entity
@Table(name="USER_DETAILS")
public class UserDetails {
/* @Id says
"userId" is primary key */
@Id @GeneratedValue
(strategy=GenerationType.AUTO)
private int userId;
private String userName;
@ElementCollection
private Set<Address> listOfAddresses = new HashSet<Address>();
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 void setListOfAddresses(Set<Address> listOfAddresses) {
this.listOfAddresses = listOfAddresses;
}
public Set<Address> getListOfAddresses() {
return listOfAddresses;
}
}
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="connection.driver_class">
org.postgresql.Driver
</property>
<property name="connection.url">
jdbc:postgresql://localhost:5433/hibernatedb
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the
built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<!-- Enable Hibernate's automatic
session context management -->
<property name="current_session_context_class">thread</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>
<!-- create / update -->
<!-- Names the annotated entity
class -->
<mapping class="org.yash.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
No comments:
Post a Comment