Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jpa - How to use @Where in Hibernate

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...