Hibernation mapping: ignore superclass field

I have class A and class B

public class A{ int f1; int f2; int f2; } public class B extends A{ } 

My question is: how to ignore a field like 'f2' in the table mapped to B?

+6
source share
2 answers

I will try to answer, assuming that the changes made to your post are approved. In the code below, I ignore the field f2 from class A, i.e. superclass B, using AttributeOverride.

 @Entity @AttributeOverride(name = "f2", column = @Column(name = "f2_col", insertable = false, updatable = false) public class B extends A{ } 

If you want to read more about this later, see AttributeOverride .

AttributeOverride with insertable = false , updatable = false should help, but it also depends on your inheritance strategy. It just helps make the matching fields inherited from the superclass transient, so some other subclass can use it, but it will be ignored for that particular subclass.

+2
source

If you want to conditionally apply fields to subclasses, perhaps you should redesign your classes so that they look like this:

 public abstract class MyAbstractClass { // ... } public class A extends MyAbstractClass { int f1; } public class B extends MyAbstractClass { int f2; } 

Assuming you serve this mapping database tables with Hibernate, you can use @MappedSuperclass on MyAbstractClass and @Entity on A and B - hope this helps.

+2
source

All Articles