如何在Hibernate中使用@Where [英] How to use @Where in Hibernate
问题描述
- PostDraft(发布是草稿,不发布)
- PostPublished(可发布帖子)
这些状态存储在一个单独的表中。显然,草稿帖子不应该是可见的。
所以我有这些类:
- 页面类(从表中获取页面信息,1页可以有多个帖子)
- 帖子类(帖子可以在草稿和发布)
在我的页面类中,我有:
@Column(name =POSTS_REF)
@Where(clause =PostPublished)
private List< Posts> userPosts;
但这不起作用!那么,如何指定一个where子句,只加载处于已发布状态的帖子而不使用JPQL ??
谢谢!
更新:2011-10-11
表格:帖子 with columns:
- id
- title
- state_ref(引用状态表的ID)
- content
Table: em> States with columns:
- id
- statename
所以我想说一句:
select *
from post内部联接状态on posts.state_ref = states.id
where states.statename ='PostPublished'
UPDATE 2011-10-13 $ b
这是我目前的修改,在我的页面类中:但它也不起作用。
/ **链接到状态* /
@JoinColumn(name =STATES_REF)
@OneToOne
@Where(clause =states.statename ='PostPublished')
公共MyState状态;
UPDATE 2012-02-13
Emt的答案适用于我试过类似:
> @Column(name =POSTS_REF)
@Where(clause =state ='PostPublished')
private List< Posts> userPosts;
或
@Column(name =POSTS_REF)
@Where(clause =PostPublished = true)
private List< Posts> userPosts;
取决于发布
实体。
Searched for a few hours, but I'm stuck in a my learning curve for PlayFramework with JPA. I'm building a sample website where posts can be made. But these posts can have the states:
- PostDraft (post is a draft, do not publish)
- PostPublished (post can be published)
These states are stored in a seperate table. Obviously, the draft state posts should not be visible yet.
So I have these classes:
- Page class (getting the page information from table, 1 page can have multiple posts)
- Posts class (posts can be in draft and published)
In my page class I have:
@Column(name="POSTS_REF")
@Where(clause="PostPublished")
private List<Posts> userPosts;
But this is not working! So, how can I specifify a where clause, to load only the posts that are in published state without using JPQL??
Thanks!
UPDATE: 2011-10-11
Table: Posts with columns: - id - title - state_ref (reference to the ID of States table) - content
Table: States with columns: - id - statename
So I want to say something like:
select *
from posts inner join states on posts.state_ref = states.id
where states.statename = 'PostPublished'
UPDATE 2011-10-13
This is my current modification, in my page class: but it does not work either.
/** link to the states */
@JoinColumn(name = "STATES_REF")
@OneToOne
@Where(clause = "states.statename = 'PostPublished'")
public MyState state;
UPDATE 2012-02-13 Emt's answer worked for me after all.
Try something like:
@Column(name="POSTS_REF")
@Where(clause="state='PostPublished'")
private List<Posts> userPosts;
or
@Column(name="POSTS_REF")
@Where(clause="PostPublished=true")
private List<Posts> userPosts;
depending on the status field type on your Post
entity.
这篇关于如何在Hibernate中使用@Where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!