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:
These states are stored in a seperate table. Obviously, the draft state posts should not be visible yet.
So I have these classes:
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.
JPQL provides a simple and straightforward way to get all entities from a table. Our Hibernate session's createQuery() method receives a typed query string as the first argument and the entity's type as the second. We execute the query with a call to the getResultList() method which returns the results as a typed List.
Short answer: A filter allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements. These filter conditions, however, can be parameterized.
We can apply the Joins in Hibernate by using the HQL query or native SQL query. To make a join between the two tables, the two tables must be in a logical relationship. We can achieve the relationship between two tables by applying the parent table's primary key as a child table's foreign key.
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.
The where clause must be a complete condition - something like this. Assuming that state is a property on the post.
@Column(name="POSTS_REF") @Where(clause="state = 'PostPublished'") private List<Posts> userPosts;
EDIT
Based on the data model - the following should work. I wouldn't recommend using it. Don't map the posts collection - just have a reference to Page from the POsts class, add a method to your DAO to retrieve the published posts for a page by using HQL or criteria query.
@Column(name="POSTS_REF") @Where(clause="exists (select id from states where state_ref = states.id and states.statename = 'PostPublished')") private List<Posts> userPosts;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With