SpringDataJpa : How to override the domain mapping defined in Parent Entity class with MappedSuperclass
Points To Remember
- Your Parent class should be annotated with
@MappedSuperclass
. - Follow the Tutorial : How to handle Inheritence with Entities to know how to wrap common properties of entities to a base class.
- To override any property you must
a. Apply the@AttributeOverride
annotation on the class that need to override the property
b. setname
property of@AttributeOverride
as the name of the field in super class.
c. set thecolumn
property of@AttributeOverride
to override the column definition of the attribute.
Let's say our Base class looks as follows
package com.ekiras.domain.base;
import javax.persistence.*;
import java.util.Date;
/**
* @author ekiras
*/
@MappedSuperclass
public abstract class BaseDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
@Temporal(TemporalType.TIMESTAMP)
protected Date dateCreated;
@Temporal(TemporalType.TIMESTAMP)
protected Date lastUpdated;
@Override
public String toString() {
return "BaseDomain{" +
"id=" + id +
", dateCreated=" + dateCreated +
", lastUpdated=" + lastUpdated +
'}';
}
// getters and setters
}
We can override the id
field of the BaseDomain
class in inheriting class as follows
package com.ekiras.domain;
import com.ekiras.domain.base.BaseDomain;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
/**
* @author ekiras
*/
@Entity
@AttributeOverride(name = "id",column = @Column(name = "userId"))
public class User extends BaseDomain{
private String email;
private String name;
private String password;
// getters and setters
}
As shown in the code above we have used @AttributeOverride
@AttributeOverride(name = "id",column = @Column(name = "userId"))
Here, name= "id"
states that we are going to override the mapping for the id
field of the super class. column = @Column(name = "userId")
states the new configuration of the id
defined in the super class. Here we simple change the name of the column for table user
defined by class User
.
This will create the table user as shown below.
mysql> show tables;
+-----------------------+
| Tables_in_jpa_mapping |
+-----------------------+
| user |
+-----------------------+
1 row in set (0.00 sec)
mysql> desc user;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| user_id | bigint(20) | NO | PRI | NULL | auto_increment |
| date_created | datetime | YES | | NULL | |
| last_updated | datetime | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
@AttributeOverride
May be applied to an entity that extends a mapped superclass or to an embedded field or
property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).
May be applied to an element collection containing instances of an embeddable class or to a map collection whose key and/or value is an embeddable class. When AttributeOverride is applied to a map, "key." or "value." must be used to prefix the name of the attribute that is being overridden in order to specify it as part of the map key or map value.
To override mappings at multiple levels of embedding, a dot (".") notation form must be used in the name element to indicate an attribute within an embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.
No comments: