Ido.Co
Ido.Co

Reputation: 5517

Make a subclass property transient

I have a class A which is extended by a subclass B. What is the best way to make one of the properties that B inherit from A transient and not persisted for class B?

When using hbm xml for configuration, transient fields are not indicated in any way and are just omitted from the xml, unlike when using annotations and the @Transient annotation.

Is this a valid solution to make x transient and not persisted for B?

public class A{    
  private Long x;
  private Long y;    

  public Long getX() {return x;}
  public Long getY() {return y;}
}

public class B extends A{    
  private Long z; 
  private transient Long x; 

  public Long getZ() {return z;}
  public Long getX() {return x;}
}

Upvotes: 0

Views: 616

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

If you're using XML configuration, you can take advantage of insert="false" and update="false" attributes:

<hibernate-mapping>
    <class name="name.of.thepackage.containing.B" table="b">
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="z" type="java.lang.Long" />
        <property name="x" type="java.lang.Long" insert="false" update="false" />
    </class>
</hibernate-mapping>

These attributes means that the field won't be used nor in INSERT nor in UPDATE SQL statements.

Upvotes: 1

Related Questions