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

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

问题描述

继以下的java文档



@Where



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



@WhereJoinTable



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



似乎注释可以以相同的方式使用:

  | --------------------- | ------ ------------- | ------------------- | 
| | @Where | @WhereTable |
| --------------------- | ------------------- | --- ---------------- |
|目标元素| TYPE,METHOD,FIELD | TYPE,METHOD,FIELD |
| --------------------- | ------------------- | --- ---------------- |
|保留|运行时间|运行时间|
| --------------------- | ------------------- | --- ---------------- |
|属性|子句|子句|
| --------------------- | ------------------- | --- ---------------- |

结果我一直很困惑,我应该知道应该使用哪个注释来处理Relation field 。我找不到使用 @Where @WhereJoinTable 之间的区别。他们两个都可以互相替换,对吗?

解决方案

第一个注解应用于目标实体。这是伪代码中这种情况的一个非常简单的例子:

  @Entity 
公共类角色{
私人长ID;
私有布尔启用;
}

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





$ b

结果只有启用的角色才会从数据库填充到User.roles集合中。

第二个注释应用于关联表。下面是伪代码中的另一个例子,但现在我们假设关联表并不像第一种情况那样微不足道:

  @实体
public class Role {
private Long id;
私有布尔启用;
}

@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 )在valid_from和valid_until之间)
private Set< Role> roles = new LinkedHashSet<>(0);
}

并且关联表具有有效性属性,例如

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

结果只有启用且有效的角色将从数据库填充到用户。角色集合。


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?

解决方案

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.

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

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