Skip to main content

AttributeOverrides and AttributeOverride annotations in hibernate

@AttributeOverride annotations allow you to override the columns to which the embedded
class’s properties are mapped.
For example,
say you have an entity which contains two separate instances of the same embeddable
they can’t both map to the same column names.

I.e. Let’s consider Adress Object which has attribute Streetname,city, pincode.
user may have many address such as Home address and office address.
in such case we use @AttributeOverrides to Override attribute
like street_name to Home_street_name
city to home_city and pincode to Home_pincode.

Create below file


1. Address.java
2. User_information.java
3. Hibernate.cfg.xml
4. EmployeeData.java

Address.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.spy.hibernate.AttributeOverride;
 
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
 
@Embeddable
public class Address 
{
	@Column(name="Office_street")
	String streetname;
 
	@Column(name="Office_society")
	String society;
 
	@Column(name="Office_city")
	String city;
 
	@Column(name="Office_pincode")
	int pincode;
 
	public String getStreetname() 
	{
		return streetname;
	}
	public void setStreetname(String streetname) 
	{
		this.streetname = streetname;
	}
 
	public String getSociety() 
	{
		return society;
	}
 
	public void setSociety(String society) 
	{
		this.society = society;
	}
 
	public String getCity() 
	{
		return city;
	}
 
	public void setCity(String city) 
	{
		this.city = city;
	}
 
	public int getPincode() 
	{
		return pincode;
	}
 
	public void setPincode(int pincode) 
	{
		this.pincode = pincode;
	}
 
}

Employee_information.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.spy.hibernate.AttributeOverride;
 
import java.util.Date;
 
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.crypto.Data;
 
 
@Entity
@Table(name="emp_data)
public class Employee_information 
{
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int id;
 
	String name;
 
	@Embedded
	@AttributeOverrides(
			{
			@AttributeOverride(name="streetname",column=@Column(name="homestreet")),
        	        @AttributeOverride(name="society",column=@Column(name="HomeSociety")),
                        @AttributeOverride(name="city",column=@Column(name="homecity")),
	                @AttributeOverride(name="pincode",column=@Column(name="homepincode")),	
	        }
	                  )
 
	Address homeaddress;
 
	@Embedded
	Address officeadress;
 
 
 
	public Address getHomeaddress() 
	{
		return homeaddress;
	}
	public void setHomeaddress(Address homeaddress) 
	{
		this.homeaddress = homeaddress;
	}	
	public Address getOfficeadress() 
	{
		return officeadress;
	}
	public void setOfficeadress(Address officeadress) 
	{
		this.officeadress = officeadress;
	}
 
	Date joindate
 
	public int getId() 
	{
		return id;
	}
	public void setId(int id) 
	{
		this.id = id;
	}
	public String getName() 
	{
		return name;
	}
 
	public Date getJoindate() 
	{
		return joindate;
	}
 
	public void setJoindate(Date joindate) 
	{
		this.joindate = joindate;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
}

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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
 
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
 
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
 
 
 
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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>
 
 
<mapping class="com.spy.hibernate.AttributeOverride.Employee_information"/>
</session-factory>
 
</hibernate-configuration>

EmployeeData.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
package com.spy.hibernate.AttributeOverride;
 
import java.util.Date;
import javax.persistence.Embeddable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class EmployeeData 
{
 
	public static void main(String[] args) 
	{
 
		Employee_information info=new Employee_information();
 
		info.setName("bhavesh");
 
 
		Address home=new Address();
 
		home.city="surat";
		home.streetname="krishna Avenue";
		home.pincode=1234;
		home.society="orchid plaza";
 
		info.setHomeaddress(home);
 
		Address office=new Address();
 
		office.city="bangalore";
		office.streetname="jpnager";
		office.pincode=12345;
		office.society="xyz";
 
		info.setOfficeadress(office);
 
 
		info.setJoindate(new Date());
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
	        Session session=sf.openSession();
 
	        session.beginTransaction();
	        session.save(info);
 
	        session.getTransaction().commit();
 
	        session.close();
 
	}
 
}

Output:
Hibernate: insert into emp_data
(homecity, homepincode, HomeSociety, homestreet, joindate, name, Office_city, Office_pincode,Office_society, Office_street)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Emp_data table
attribute-override