Address.java
package com.hibernate.model;
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="DISTRICT_NAME")
private String
district;
@Column(name="COUNTRY_NAME")
private String
country;
@Column(name="POSTAL_CODE")
private Integer
postalcode;
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
getDistrict() {
return district;
}
public void setDistrict(String
district) {
this.district
= district;
}
public String
getCountry() {
return country;
}
public void setCountry(String
country) {
this.country
= country;
}
public Integer
getPostalcode() {
return postalcode;
}
public void setPostalcode(Integer
postalcode) {
this.postalcode
= postalcode;
}
}
UserDetails.java
package com.hibernate.model;
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
= "userdetails")
public class UserDetails
{
@Id
@GeneratedValue(strategy
= GenerationType.AUTO)
private Integer
userId;
private String
userName;
@ElementCollection
private Set<Address>
listOfAddresses = new HashSet<Address>();
public Set<Address>
getListOfAddresses() {
return listOfAddresses;
}
public void setListOfAddresses(Set<Address>
listOfAddresses) {
this.listOfAddresses
= listOfAddresses;
}
public Integer
getUserId() {
return userId;
}
public void setUserId(Integer
userId) {
this.userId
= userId;
}
public String
getUserName() {
return userName;
}
public void setUserName(String
userName) {
this.userName
= userName;
}
}
UserAccess.java
package com.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.model.Address;
import com.hibernate.model.UserDetails;
public class UserAccess
{
public static void main(String[]
args) {
// TODO Auto-generated
method stub
UserDetails
user = new UserDetails();
user.setUserName("karthik
reddy");
Address
address1 = new Address();
address1.setStreet("A");
address1.setCity("Irala");
address1.setDistrict("Chittoor");
address1.setCountry("India");
address1.setPostalcode(517130);
Address
address2 = new Address();
address2.setStreet("B");
address2.setCity("Kanipakam");
address2.setDistrict("Chittoor1");
address2.setCountry("India");
address2.setPostalcode(517135);
user.getListOfAddresses().add(address1);
user.getListOfAddresses().add(address2);
SessionFactory
factory = new Configuration().configure()
.buildSessionFactory();
Session
session = factory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
factory.close();
}
}
hibernate.cfg.xml
<?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.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property
name="connection.username">system</property>
<property
name="connection.password">system</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>
<!--
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>
<mapping class="com.hibernate.model.UserDetails" />
</session-factory>
</hibernate-configuration>
No comments:
Post a Comment