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

Christian Beikov christian.beikov at gmail.com
Thu Jul 9 03:44:44 EDT 2020


I agree, this should work. Do you have a JIRA ticket describing what the 
problem is?

Am 09.07.2020 um 02:42 schrieb Gail Badner:
> 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
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev


More information about the hibernate-dev mailing list