[hibernate-dev] Can a transient attribute be overridden to be persistent in a subclass?

Gail Badner <[email protected]>
Newsgroups gmane.comp.java.hibernate.devel
Message-ID <CAFDzhiTFvVJGnHYmmaFY=KTpZCkdfFGKeVXsw9o2SGmtxXZ5qQ@mail.gmail.com>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.