[hibernate-dev] Can a transient attribute be overridden to be persistent in a subclass?
Gail Badner
gbadner at redhat.com
Wed Jul 8 20:42:18 EDT 2020
I *think* the following is valid, but I need confirmation. This mapping
does seem to work for Hibernate.
A transient attribute, Employee#title, gets overridden as a persistent
attribute, Editor#title, stored in a column named "e_title".
Writer#title is also transient, but Writer#group uses the same "e_title"
column as a foreign key. That foreign key value is used to populate
Writer#title.
@Entity
@Table(name="Employee")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="department")
public abstract class Employee {
private String name;
private String title;
@Id
public String getName() {
return name;
}
@Transient
public String getTitle() {
return title;
}
protected void setName(String name) {
this.name = name;
}
protected void setTitle(String title) {
this.title = title;
}
}
@Entity
public class Editor extends Employee {
@Column(name = "e_title")
public String getTitle() {
return super.getTitle();
}
public void setTitle(String title) {
super.setTitle( title );
}
}
@Entity
public class Writer extends Employee {
private Group group;
@ManyToOne(optional = false)
@JoinColumn(name = "e_title")
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
setTitle( group.getName() );
}
}
In 4.2, the above mapping worked with Employee#title being persistent, but
does not work in 5.3.
Section 2.2 Persistent Fields and Properties of the spec says:
"Entity subclasses may override the property accessor methods. However,
portable applications must not override the object/relational mapping
metadata that applies to the persistent fields or properties of entity
superclasses."
Would overriding a transient attribute be portable? If not, is that
something Hibernate would/should support?
Thanks,
Gail
More information about the hibernate-dev
mailing list