开发者

Is it possible to add JPA annotation to superclass instance variables?

开发者 https://www.devze.com 2022-12-30 23:21 出处:网络
I am creating entities that arethe same for two different tables. In order do table mappings etc. different for the two entities but only have the rest of the code in one place - an abstract superclas

I am creating entities that are the same for two different tables. In order do table mappings etc. different for the two entities but only have the rest of the code in one place - an abstract superclass. The best thing would be to be able to annotate generic stuff such as column names (since the will be identical) in the super class but that does not work because JPA annotations are n开发者_C百科ot inherited by child classes. Here is an example:

public abstract class MyAbstractEntity {

  @Column(name="PROPERTY") //This will not be inherited and is therefore useless here
  protected String property;


  public String getProperty() {
    return this.property;
  }

  //setters, hashCode, equals etc. methods
}

Which I would like to inherit and only specify the child-specific stuff, like annotations:

@Entity
@Table(name="MY_ENTITY_TABLE")
public class MyEntity extends MyAbstractEntity {

  //This will not work since this field does not override the super class field, thus the setters and getters break.
  @Column(name="PROPERTY") 
  protected String property;

}

Any ideas or will I have to create fields, getters and setters in the child classes?

Thanks, Kris


You might want to annotate MyAbstractEntity with @MappedSuperclass class so that hibernate will import the configuration of MyAbstractEntity in the child and you won't have to override the field, just use the parent's. That annotation is the signal to hibernate that it has to examine the parent class too. Otherwise it assumes it can ignore it.


Here is an example with some explanations that may help.

@MappedSuperclass:

  • Is a convenience class
  • Is used to store shared state & behavior available to child classes
  • Is not persistable
  • Only child classes are persistable

@Inheritance specifies one of three mapping strategies:

  1. Single-Table
  2. Joined
  3. Table per Class

@DiscriminatorColumn is used to define which column will be used to distinguish between child objects.

@DiscriminatorValue is used to specify a value that is used to distinguish a child object.

The following code results in the following:

Is it possible to add JPA annotation to superclass instance variables?

You can see that the id field is in both tables, but is only specified in the AbstractEntityId @MappedSuperclass.

Also, the @DisciminatorColumn is shown as PARTY_TYPE in the Party table.

The @DiscriminatorValue is shown as Person as a record in the PARTY_TYPE column of the Party table.

Very importantly, the AbstractEntityId class does not get persisted at all.

I have not specified @Column annotations and instead are just relying on the default values.

If you added an Organisation entity that extended Party and if that was persisted next, then the Party table would have:

  • id = 2
  • PARTY_TYPE = "Organisation"

The Organisation table first entry would have:

  • id = 2
  • other attribute value associated specifically with organisations

    @MappedSuperclass
    @SequenceGenerator(name = "sequenceGenerator", 
            initialValue = 1, allocationSize = 1)
    public class AbstractEntityId implements Serializable {
    
        private static final long serialVersionUID = 1L;
        
        @Id
        @GeneratedValue(generator = "sequenceGenerator")
        protected Long id;
        
        public AbstractEntityId() {}
        
        public Long getId() {
            return id;
        }
    }
    
    @Entity
    @Inheritance(strategy = InheritanceType.JOINED)
    @DiscriminatorColumn(name = "PARTY_TYPE", 
            discriminatorType = DiscriminatorType.STRING)
    public class Party extends AbstractEntityId {
        
        public Party() {}
        
    }
    
    @Entity
    @DiscriminatorValue("Person")
    public class Person extends Party {
        
        private String givenName;
        private String familyName;
        private String preferredName;
        @Temporal(TemporalType.DATE)
        private Date dateOfBirth;
        private String gender;
        
        public Person() {}
    
        // getter & setters etc.
     
    }

Hope this helps :)


Mark the superclass as

@MappedSuperclass

and remove the property from the child class.


Annotating your base class with @MappedSuperclass should do exactly what you want.


This is old, but I recently dealt with this and would like to share my solution. You can add annotations to an overridden getter.

@MappedSuperclass
public abstract class AbstractEntity<ID extends Serializable> implements Serializable {

  @Column(name = "id", nullable = false, updatable = false)
  @Id
  private ID id;

  public ID getId() {
    return id;
  }

  ...

}
@Entity
@Table(name = "address")
public final class Address extends AbstractEntity<UUID> implements Serializable {

  ...

  @Override
  @GeneratedValue(generator = "UUID")
  @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
  public final UUID getId() {
    return super.getId();
  }

  ...

}
0

精彩评论

暂无评论...
验证码 换一张
取 消