Hibernate annotations. @Where vs @WhereJoinTable

社会主义新天地 提交于 2019-12-21 20:12:00

问题


Following java doc

@Where

Where clause to add to the element Entity or target entity of a collection. The clause is written in SQL. A common use case here is for soft-deletes.

@WhereJoinTable

Where clause to add to the collection join table. The clause is written in SQL. Just as with {@link Where}, a common use case is for implementing soft-deletes.

It seems annotations can be used in same way in general:

|---------------------|-------------------|-------------------|
|                     |@Where             | @WhereTable       |
|---------------------|-------------------|-------------------|
|target elements      |TYPE, METHOD, FIELD|TYPE, METHOD, FIELD|
|---------------------|-------------------|-------------------|
|Retention            |RUNTIME            |RUNTIME            |
|---------------------|-------------------|-------------------|
|properties           |clause             |clause             |
|---------------------|-------------------|-------------------|

And as result I've been really confused how I should know which annotation I should use for Relation field. I can't find difference between using of @Where and @WhereJoinTable. Both of them can replace each other, am I right?


回答1:


First annotation is applied on target entity. Here is very simplified example of this case in pseudo code:

@Entity
public class Role {
    private Long id;
    private boolean enabled;
}     

@Entity
public class User {
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    @Where(clause = "enabled = true")
    private Set<Role> roles = new LinkedHashSet<>(0);
}

As result only enabled roles will be populated from the database into User.roles collections.

Second annotation is applied on the association table. Below is another example in pseudo-code, but now we suppose that association table is not that trivial as in first case:

@Entity
public class Role {
    private Long id;
    private boolean enabled;
}   

@Entity
public class User {
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    @Where(clause = "enabled = true")
    @WhereJoinTable(clause = "now() between valid_from and valid_until")
    private Set<Role> roles = new LinkedHashSet<>(0);
}

and association table has validity attributes, something like 

CREATE TABLE USER_ROLE {
    ID NUMBER NOT NULL,
    USER_ID NUMBER NOT NULL,
    ROLE_ID NUMBER NOT NULL,
    VALID_FROM DATETIME,
    VALID_UNTIL DATETIME
} 

As result only enabled and valid roles will be populated from the database into User.roles collections.



来源:https://stackoverflow.com/questions/45504002/hibernate-annotations-where-vs-wherejointable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!