开发者

Hibernate storing derived property

开发者 https://www.devze.com 2023-03-08 08:36 出处:网络
I have a class persisted by Hibernate with a derived property isComplete. @Entity class Container { @Id @GeneratedValue(strategy = GenerationType.AUTO)

I have a class persisted by Hibernate with a derived property isComplete.

@Entity
class Container {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column private String oneFish;
    @Column private String twoFish;
    @Column private String redFish; 
    @Column private String blueFish;

    public Boolean isComplete(){
        return oneFish != null 
               && twoFish != null  
               && redFish != null 
               && blueFish != null;
    }
}

How do I get Hibernate to persist isComplete t开发者_开发问答o the database so that other (non-hibernate) access can see the value?


If you annotated the method with @Column it will get persisted (you may have to specify @AccessType / @Access on the entity)

But you shouldn't do that. Store all the other values and compute the isComplete whenever you need it. It is not an expensive operation. You can even 'cache' it in a @Transient Boolean property, but it need not go the the database.


I'm using XML Hibernate mappings, but if I understand Hibernate annotations correctly, you can annotate isComplete() as @Column and provide a do-nothing setter setComplete().

0

精彩评论

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