休眠注释.@Where vs @WhereJoinTable [英] Hibernate annotations. @Where vs @WhereJoinTable

查看:27
本文介绍了休眠注释.@Where vs @WhereJoinTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 java 文档

Where 子句添加到元素实体或集合的目标实体.该子句是用 SQL 编写的.这里的一个常见用例是软删除.

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.

Where 子句添加到集合连接表.该子句是用 SQL 编写的.就像 {@link Where} 一样,一个常见的用例是实现软删除.

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             |
|---------------------|-------------------|-------------------|

结果我真的很困惑我应该如何知道我应该为 Relation 字段使用哪个注释.我找不到使用 @Where@WhereJoinTable 之间的区别.两者可以互相替换,对吗?

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?

推荐答案

第一个注释应用于目标实体.这是这种情况的伪代码非常简化的示例:

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);
}

因此,只有启用的角色才会从数据库填充到 User.roles 集合中.

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
} 

因此,只有启用且有效的角色才会从数据库填充到 User.roles 集合中.

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

这篇关于休眠注释.@Where vs @WhereJoinTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆