代码之家  ›  专栏  ›  技术社区  ›  Aravind Yarram

JPA不支持很好的接口..影响?

  •  6
  • Aravind Yarram  · 技术社区  · 15 年前

    我在JPA的stackoverflow中浏览了一些帖子,我读了很多JPA不支持接口的地方。有人能分享一下它在现实世界中的意义吗?这是否意味着我们不能注释接口?

    3 回复  |  直到 15 年前
        2
  •  10
  •   Sean Patrick Floyd    15 年前

    use @MappedSuperclass for inheritance

    @MappedSuperclass
    public abstract class BaseEntity implements Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    }
    

    @MappedSuperclass
    public abstract class ExtendedEntity extends BaseEntity{
    
        @Temporal(TemporalType.TIMESTAMP)
        private Date createdDate;
    
        @Temporal(TemporalType.TIMESTAMP)
        private Date updatedDate;
    
        @PrePersist
        protected void creationTimeStamp(){
            createdDate = new Date();
            this.updateTimeStamp();
        }
    
        @PreUpdate
        protected void updateTimeStamp(){
            updatedDate = new Date();
        }
    
    }
    

    BaseEntity ExtendedEntity

    the way the inheritance is modeled

    perhaps @Embeddable is the solution

        3
  •  0
  •   duffymo    15 年前