Hibernate过滤器oneToMany关系包含对象的实体 [英] Hibernate filter Entity where oneToMany relation contains an object
问题描述
我想使用hibernate过滤器,但我不知道自己想做什么是可能的。
我有2个实体:
Message和MessageUser。
消息有一个MessageUser列表。
我想创建一个过滤器,所以我可以做这样的事情:
final Session filteredSession = sessionFactory.openSession();
final Filter filter = filteredSession.enableFilter(userRecipient);
filter.setParameter(userRecipient,myUser);
filter.validate();
最终列表<消息> userMessages = filteredSession.createQuery(from Message)。list();
它仅返回myUser是收件人的消息吗?
是否有可能以及如何?
非常感谢!
如果您对标准感兴趣,可以创建如下标准:
Session hbSession = sessionFactory.openSession ();
Criteria criteria = hbSession.createCriteria(Message.class);
criteria.createCriteria(msgUserList,userListAlias); // msgUserList是Message
criteria.add(Restrictions.eq(userListAlias.user,myUser))中用户列表的变量名称。 //对于msgUserList类中的用户类型,用户是可变的。
列表<消息> userMessages = criteria.list();
查看这个供创建条件时参考!
如果你只想使用过滤器,然后我希望你已经在你的用户
List
有点像bellow
通过* .hbm.xml
< hibernate- mapping package =com ....>
< class name =Messagetable =message_table>
....
< list name =msgUserListinverse =truecascade =all>
< key column =user_id/>
< filter name =userRecipientcondition =user_id =:userParam/>
< / list>
< / class>
< filter-def name =userRecipient>
< filter-param name =userParamtype =User/> // User is class
< / filter-def>
< / hibernate-mapping>
或通过注释
@Entity
@FilterDef(name =userRecipient,
parameters = @ ParamDef(name =userParam,type =PAKAGE.User ))
@Table(name =message_table,catalog =your_db)
public class Message {
...
@OneToMany(fetch = FetchType .LAZY,mappedBy =stock)
@Filter(name =userRecipient,condition =user =:userParam)
public List< MessageUser> msgUserList;
在此之后,您将可以使用过滤器工作
Filter filter = session.enableFilter(userRecipient);
filter.setParameter(userParam,myUser);
更新
< 过滤器 code>
与不同 Criteria
,根据我的理解,可以说 Filter
就像一个已经应用的Criteria你的类或集合在和 off
开关上有。如果您的hibernate会话启用了某个过滤器,并且该过滤器的参数设置为
on
,并且与指定此过滤器的类或集合有关的所有查询都将始终返回过滤结果为根据条件。这意味着您不必每次都明确定义它,并且通过使用 getEnabledFilter(filterName)
,您可以随时更改该过滤器的参数。
过滤器的示例用法可以是如果您有 中执行的电影。另一方面,您可以在需要数据库结果不需要复制的条件,而不希望稍后在hibernate会话中复制它。像 我希望你了解过滤器和标准的基本概念,并且从我对你的问题的理解中,如果你使用标准,它会更好! I want to use the hibernate filter but I don't know if what I want to do is possible I have 2 entities : Message and MessageUser. A Message has a list of MessageUser. I want to create a filter so I can do something like : it returns me only the message where myUser is the recipient ? is it possible to to and how? Thanks a lot ! If you are comfortable with Criteria you could create criteria like this Have a look at this for reference while creating criteria! If you only want to use filter then I hope you have configured filter on your By *.hbm.xml Or By annotation after this you will be able get your filter working Update Purpose of Example usage of filter can be if you have Criteria on the other hand you can use when you require result from database with certain conditions which does not need to be replicated rather you don't want it to be replicated later in the hibernate session. Like I hope you understood basic concept of filter and criteria, and from my understanding of your problem it will be better if you use criteria! 这篇关于Hibernate过滤器oneToMany关系包含对象的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Movies
表和 Actor
具有多对多关系的表格,如 Leonardo Dicaprio
可以在同一时间有很多电影 titanic
可以有很多演员,当你得到 Actor
时,显然你只想要那些 Actor
已执行,因此您可以在此处使用过滤器,该过滤器应用于映射到中的
类。通过这种方式获得 Movies
演员 Actor
对象时,只需使用名称的简单条件,而不是其他任何东西,并访问它< Movie c
运算符放在 Actor中
对象,它将只返回你演员执行过的电影。这也意味着无论当您访问 Movie
集合时,您是如何从数据库获得
Actor 演员
它会为您提供演员在
演员
可以说 Leonardo Dicaprio
包含电影
的集合在奥斯卡提名他。这个集合只有在经过某些条件后才会在 Actor中填充,并且不会在其他
Actor中使用
对象没有被这个标准检索。
final Session filteredSession = sessionFactory.openSession();
final Filter filter = filteredSession.enableFilter("userRecipient");
filter.setParameter("userRecipient", myUser);
filter.validate();
final List<Message> userMessages = filteredSession.createQuery("from Message").list();
Session hbSession= sessionFactory.openSession();
Criteria criteria = hbSession.createCriteria(Message.class);
criteria.createCriteria("msgUserList","userListAlias");// msgUserList is variable name of users list in Message
criteria.add(Restrictions.eq("userListAlias.user",myUser));//user is variable for User type in msgUserList's class.
List<Message> userMessages = criteria.list();
User
List
some thing like bellow<hibernate-mapping package="com....">
<class name="Message" table="message_table">
....
<list name="msgUserList" inverse="true" cascade="all">
<key column="user_id" />
<one-to-many class="MessageUsers" />
<filter name="userRecipient" condition="user_id =:userParam" />
</list>
</class>
<filter-def name="userRecipient">
<filter-param name="userParam" type="User" />//User is class
</filter-def>
</hibernate-mapping>
@Entity
@FilterDef(name="userRecipient",
parameters=@ParamDef(name="userParam", type="PAKAGE.User" ))
@Table(name = "message_table", catalog = "your_db")
public class Message{
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
@Filter(name = "userRecipient",condition="user = :userParam")
public List<MessageUser> msgUserList;
Filter filter = session.enableFilter("userRecipient");
filter.setParameter("userParam", myUser);
Filter
is different than the Criteria
, from my understanding you can say that Filter
is just like a Criteria that is already applied on your class or collection which has on
and off
switch. If your hibernate session has certain filter enabled with it's parameters set than that filter is on
and all queries relating to the class or collection which has this filter specified will always return filtered result as per the condition. This means you don't have to explicitly define it every time and by using getEnabledFilter("filterName")
you can just change that filter's parameters any time. Movies
table and Actor
table with many-to-many relationship, like Leonardo Dicaprio
can have many movies at the same times A titanic
can have many actors, here when you get Actor
obviously you would want only those movies which this Actor
has performed in, so you can use filter here which is applied on collection of Movies
that is mapped in Actor
class. This way when you get Actor
object say by simple criteria of name and nothing else and access it's Movie
collection by .
operator on Actor
object it will return you only movies which that actor has performed. This also means no matter how you got Actor
object from database when you access Movie
collection of Actor
it will provide you movies that this actor has performed inActor
lets say Leonardo Dicaprio
containing collection of Movies
that got nominated him in Oscar. This collection will only be populated in Actor
object when gone through certain criteria and will not be available on other Actor
objects which have not being retrieved by this criteria.