jpql

JPQL query for ManyToMany relationship?

Hi all, I have two entities: Student and Course with manytomany relationship. I wanna give a student a list of the courses he did not choose yet. For example, If we have 6 courses, and stduent A has selected course 1 , 3, 5, we should list course 2,4 in the available courses list for him. What's the right JPQL for that? Thanks. ...

MEMBER OF in EJB-QL 3 doesn't work

Hi, I would like to retrieve many 'Access' which have one 'Role' in common. It's the Named Querie SELECT access FROM Access AS access WHERE :role MEMBER OF access.listRole The Access Entity public class Access implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String...

JP QL - Filtering result in a One To Many relationship

I am stuck trying to construct a JPQL query and was hoping someone with more JPA experience than mine could help. Consider the following two entities: class Author{ String name @OneToMany(mappedBy="author") Set<Book> books } class Book{ String title Boolean inPrint @ManyToOne Author author } If I want to return a spec...

select from two tables using JPQL

Hi I m using JPQL to retrieve data. I can get data using the statement List persons = null; persons = em.createQuery("select p.albumName from PhotoAlbum p , Roleuser r where r = p.userId and r.userID = 1"); Now i can get the album names using this... int i=0; for (i=0;i<persons.size(); i++) { System.out.println("Testing n "...

JPA Fetch Join Filter on List/Set

I'm having problem on JPA (Hibernate) Fetch Join : Here is my JPQL query SELECT n FROM News n LEFT JOIN FETCH n.profiles AS pr WHERE pr.id=?1 But it's not working. How can I make a query that filters on the list that is being fetched ? ...

JPQL ORDER BY clause with parameter

I'm trying to write a JPQL Query with an ORDER BY clause: query = "SELECT c FROM item ORDER BY c.name ASC" I would like to set an "order" parameter, whose value would be either "ASC" or "DESC": query = "SELECT c FROM item ORDER BY c.name :order" And then in my implementation: query.setParameter("order", "ASC"); This is when I ge...

How do I do a JPQL SubQuery?

It is possible to do the equivalent of this sql query in JPQL? SELECT * FROM COUNTRIES c WHERE COUNTRY_ID IN ( SELECT DISTINCT COUNTRY_ID FROM PORTS p WHERE p.COUNTRY_ID = c.COUNTRY_ID AND STATE = 'A' ) ...

How to Assign a JPQL query to a simple Java object?

I need to assign the result of a jpql result to a simple class java object I have something like this class myObject() { @id private Long id; private String Name; private String description; ... //getters and setters } I need to somehow to store the result of this SQL query, example // could be anytable SELECT DISTINCT c.table_i...

Recursive JPA query?

Does JPA 2 have any mechanism for running recursive queries? Here's my situation: I have an entity E, which contains an integer field x. It also may have children of type E, mapped via @OneToMany. What I'd like to do is find an E by primary key, and get its value of x, along with the x values of all its descendants. Is there any way to ...

JPQL MySQLSyntaxErrorException: You have an error in your SQL syntax

Hello, I have two entities User and Group: @Entity public class User implements Serializable { private static final long serialVersionUID = 1L; @Id private String username; private String password; private String email; @ManyToMany @JoinTable(name="user_group", joinColumns={@JoinColumn(name="USERNAME")}, inv...

JPA Query optimization by avoiding JOIN to lookup table?...

Imagine a table emp: CREATE TABLE emp ( id NUMBER , name VARCHAR , dept_code VARCHAR ) and a table dept: CREATE TABLE dept ( code VARCHAR , name VARCHAR ) emp.dept_code references dept.code as a ForeignKey. These tables are mapped to JPA Entities, and the ForeignKey is modeled as an association...

Sophisticated JPQL String Query

I am trying to execute a pretty-sophisticated query on a string field in the database. I am not very experienced at JPQL, so I thought I would try to get some help. I have a field in the database called FILE_PATH. Within the FILE_PATH field, there will be values such as: 'C:\temp\files\filename.txt' 'file:\\\C:\testing\testfolder\inn...

JPQL LIKE expression on enum

Can JPQL execute LIKE expressions against enums? If I have an entity Foo with an enum field bar I can execute the following in MySQL(bar is stored as a MySQL enum)... SELECT * FROM Foo WHERE `bar` LIKE '%SUFFIX' However, the corresponding query in JPQL... SELECT f FROM Foo f WHERE f.bar LIKE '%SUFFIX' ...complains that... Param...

JPA Query MONTH/YEAR functions

How can I write a JPA query using MONTH function just like sql query? @NamedQuery(name="querybymonth", query="select t from table1 t where MONTH(c_Date) = 5") When I use the above pattern for query, I get an error: unexpected token - MONTH. ...

Oracle CLOB and JPA/Hibernate ORDER BY?

I have a JPQL query that works fine with MySQL and SQL Server. But with Oracle it fails with ORA-00932: inconsistent datatypes: expected - got CLOB. The reason seems to be that Oracle does not support ORDER BY with CLOB columns. Is there any JPQL work around for this? ...

How do I delete a row in a join table automatically, to avoid a ConstraintViolationException?

This seems like it should be a pretty simple question, or at least have a simple answer. But - I'm really not a database guy, and I'm still pretty far down on the Hibernate learning curve. That said, here's the setup: Consider a unidirectional many-to-many relationship between two entities, from Foo to Bar: (pardon any typos below, th...

JPA: JOIN in JPQL

I thought I know how to JOIN in JPQL but apparently not. Can anyone help me? select b.fname, b.lname from Users b JOIN Groups c where c.groupName = :groupName This give me Exception org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing the query Internal Exception: org.eclipse.persistence.intern...

How do I query for only superclass entities in a jpql query?

I have the following entities: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="orderType", discriminatorType=DiscriminatorType.STRING) @DiscriminatorValue(value="BASE") @Table(name = "orders") public class OrderEntity implements Serializable { ... and @Entity @DiscriminatorValue(value="RECURRING...

unexpected JPA results

Hi, Im learning JPA and having problems. My main problem is when i join my entitys and dont quite understand why im getting the results i am .. and its realy slowing my progress if someone could help i would be very greatful. Entitys College @Entity @NamedQueries({ @NamedQuery(name=College.allCollegeQuery,query="SELECT col FROM C...

Selecting specific fields in JPA Queries

I am using Hibernate JPA implementation org.hibernate:hibernate-entitymanager:3.4.0.GA at central maven repo. I have two classes. Class A extends Class B When I execute a query like "SELECT b FROM B b", the result list, as expected, contains Class A instances too. Is there a way to retrieve only the Class B instances without any fie...