Sunday, 20 January 2013

How to use @AttributeOverrides and @AttributeOverride annotations in hibernate

//In this program suppose we have some address now we want to add some other address without modifying the address table


Address.java


package com.hibernate.model;

import javax.persistence.Embeddable;

@Embeddable
public class Address {
private String street;
private String city;
private String district;
private String country;
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 javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "userdet")
public class UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer userId;
private String userName;

@Embedded
@AttributeOverrides({
@AttributeOverride(name = "street", column = @Column(name = "HOME_STREET_NAME")),
@AttributeOverride(name = "city", column = @Column(name = "HOME_CITY_NAME")),
@AttributeOverride(name = "district", column = @Column(name = "HOME_DISTRICT_NAME")),
@AttributeOverride(name = "country", column = @Column(name = "HOME_COUNTRY_NAME")),
@AttributeOverride(name = "postalcode", column = @Column(name = "HOME_POSTALCODE_NAME"))

})
private Address homeAddress;
@Embedded
private Address officeAddress;

public Address getOfficeAddress() {
return officeAddress;
}

public void setOfficeAddress(Address officeAddress) {
this.officeAddress = officeAddress;
}

public Address getHomeAddress() {
return homeAddress;
}

public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}

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 address = new Address();
address.setStreet("Kattamanchi");
address.setCity("Chittoor");
address.setDistrict("Chittoor");
address.setCountry("India");
address.setPostalcode(517130);

Address address1 = new Address();
address1.setStreet("ilavaripalli");
address1.setCity("irala");
address1.setDistrict("Bangalore");
address1.setCountry("America");
address1.setPostalcode(512130);

SessionFactory factory = new Configuration().configure()
.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
user.setHomeAddress(address);
user.setOfficeAddress(address1);
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">update</property>
<mapping class="com.hibernate.model.UserDetails" />


</session-factory>

</hibernate-configuration>








3 comments: