@AttributeOverrides and @AttributeOverride Annotations
In our previous example we had seen how to inject one entity into another entity(here). We have injected the address class into employee class. In that example we have not renamed the fields in the address class (city, state and pincode). We accepted the default field names – city, state and pincode.

In this example we are going to see, how we are going to change the name of the fields for the members in address class. There are 2 ways of doing it,

Method 1:
This method is similar to previous example. Just like we used @Column annotation in employee class, we can use the same annotation in address class and rename the database column name.

Method 2:
We can change the name of the column for the members in address class using @AttributeOverrides and @AttributeOverride annotations.

Method 1 has already been explained in previous example, Lets see the example for Method 2 in this article.



    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">URL</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.show_sql">true</property>
    <mapping class="com.javainfinite.pojo.employee"/>


package com.javainfinite.pojo;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    public static SessionFactory getSessionFactory() {
        return sessionFactory;


package com.javainfinite.pojo;

import javax.persistence.Embeddable;

public class address {
    private String state;
    private String city;
    private String pincode;

    public String getState() {
        return state;

    public void setState(String state) {
        this.state = state;

    public String getCity() {
        return city;

    public void setCity(String city) {
        this.city = city;

    public String getPincode() {
        return pincode;

    public void setPincode(String pincode) {
        this.pincode = pincode;


package com.javainfinite.pojo;

import java.io.Serializable;
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.Id;
import javax.persistence.Table;

@Table (name="Employee")
public class employee implements Serializable {
    private int eid;
    private String ename;
    private String epass;
    @AttributeOverrides (
    @AttributeOverride (name="state", column=@Column(name="Street_Override")),
    @AttributeOverride (name="city", column=@Column(name="City_Override")),
    @AttributeOverride (name="pincode", column=@Column(name="Pincode_Override"))
    private address address1;

    public address getAddress1() {
        return address1;

    public void setAddress1(address address1) {
        this.address1 = address1;
    public int getEid() {
        return eid;

    public void setEid(int eid) {
        this.eid = eid;

    public String getEname() {
        return ename;

    public void setEname(String ename) {
        this.ename = ename;

    public String getEpass() {
        return epass;

    public void setEpass(String epass) {
        this.epass = epass;

In employee.java, We have used @AttributeOverrides – This annotation is used when we have to change the column names for more than one member and @AttributeOverride is the annotation used to change the column name for each variable


package com.javainfinite.exec;

import com.javainfinite.pojo.NewHibernateUtil;
import com.javainfinite.pojo.address;
import com.javainfinite.pojo.employee;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Execution {
    public static void main(String args[])
        Session session=NewHibernateUtil.getSessionFactory().openSession();
            Transaction transaction=session.beginTransaction();
            employee emp=new employee();
            address addr=new address();
        catch(Exception e)


We can see that the column names has been changed to City_Override, Pincode_Override and Street_Override





By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *