@AttributeOverrides annotation in hibernate/JPA

@AttributeOverrides annotation is used in hibernate/JPA to rename the column(s) name of the embadable class while it is being embedded in other class.
Suppose A class is embedded in class B, then if A entity contains the column name of any property "phone", then B entity can override the column name to "mobileNumber". So, in the table, column name will be "mobileNumber".

package entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

public class ContactKey implements Serializable {
    private int roll;
    private String sem;
    private String branch;

    public int getRoll() {
        return roll;

    public void setRoll(int roll) {
        this.roll = roll;

    @Column(name = "sem")
    public String getSem() {
        return sem;

    public void setSem(String sem) {
        this.sem = sem;

    public String getBranch() {
        return branch;

    public void setBranch(String branch) {
        this.branch = branch;

    public String toString() {
        return "ContactKey [roll=" + roll + ", sem=" + sem + ", branch="
                + branch + "]";

package entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
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.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

public class Contact {
    private    int id;
    private ContactKey contactKey;
    private String name;
    private String email;
    private long phone;
    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;

    public long getPhone() {
        return phone;
    public void setPhone(long phone) {
        this.phone = phone;
    public ContactKey getContactKey() {
        return contactKey;

    public void setContactKey(ContactKey contactKey) {
        this.contactKey = contactKey;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

here, database table will not contain the column "sem". It will have the "semester" column since we have overridden in contact entity.

No comments:

Popular Posts