jpql

JPA + EJB + JSF: how can design complicated query

I am using netbean 6.8 btw. Let say that I have 4 different tables: Company, Facility, Project, and Document. So the relationship is this. A company can have multiple facilities. A facility can have multiple projects, and a project can have multiple documents. Company: +companyNum: PK +facilityNum: FK Facility: +facilityNum: PK +pro...

Can I refer to the primary key of any entity in JPQL by the name "id", regardless of the entity's ID property name?

Hibernate allows you to just say ".id". Let's say I have: @Entity public class Customer { @Id private Integer customerId; @Basic private String customerName; // getters and setters } And I want to get the name of a customer by ID: SELECT cust.customerName FROM Customer cust WHERE cust.id = :customerId Notice I put "id" rathe...

JPA : Many to Many query help needed

I have four entities that are involved in a query that I'm having a little trouble with. The relationship is as follows : Exchange----*Contract*----*Combo----*Trade and the (simplified) entities are as follows: @Entity public class Exchange implements Serializable { @Id(name="EXCHANGE_ID") private long exchangeId; @Column ...

Doing an "IN" query with Hibernate

I have a list of IDs in a String, and want to use Hibernate to get the rows with these IDs. TrackedItem is a Hibernate/JPA entity (sorry if I'm getting the naming mixed up here). My code is: String idsText = "380, 382, 386"; ArrayList<Long> ids = new ArrayList<Long>(); for (String i : idsText.split(",")) { ids.add(Long.getLong(i))...

Returning a list from a ManyToMany relationship?

I have two tables: Users and Roles. A user may have more roles, so this is a ManyToMany relationship. I've generated the entity classes with Netbeans, and they look like this: @Table(name = "users") public class Users implements Serializable { @Id @Basic(optional = false) @Column(name = "user_name") private String userNa...

Question about JPQL, @NamedQuery

If I have something like this @Entity public class Facility { ... @ManyToOne @JoinColumn(name="CUSTOMER_FK") private Customer customer; ... } does my @NameQuery like this @NamedQuery(name="Facility.findByCustomerId", query="select c from Facility c where c.customer=:customer_fk") or like this @NamedQuery(name="...

Is there a tool that converts from JPQL into Criteria API?

Input: valid jpql output: java code using criteria api to achieve same jpql effects. ...

how to write JPA query

Hello, Learning how to write JPA query. Please advise me whether it possible to write the below queries more efficiently, may be in a single select statement. May be a join, but not sure how to do it. class Relationship { @ManyToOne public String relationshipType; //can be MANAGER, CUSTOMER etc @ManyToOne public Party party...

Help converting SQL query to JPQL

I have a SQL query that I need to translate over into JPQL, and I'm beginning to wonder if this is one of the cases where I'll just have to use native SQL. For reference, here is the SQL query. SELECT c.title, a.approval_total, r.requested_total FROM codes c INNER JOIN (SELECT code_id, year, SUM(requested_amount) requestedTotal ...

JPA: When parent entity got removed, child entity still remain

Customer Entity (Parent Entity) @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @OneToMany(mappedBy="customer", cascade=CascadeType.ALL) private List<Facility> facilities; //Setter and Getter for name and facilities public void addFacility(Facility facility){...

Why is an object found by id in JPA, but not through a JPQL query?

I have a JUnit 4 test case with the Spring @Transactional annotation that saves an object, and then attempts to find it. The test case passes when I use this implementation: @Override public EventSummary findEventSummaryById(Integer id) { return em.find(EventSummary.class, id); } It fails when I use this implementation (and then c...

Using group by and having in a JPA query

How would you do the following using the JPA query language? select * from person where email in (select email from person group by email having count(email)>1) ...

Are SQL injection attacks possible in JPA?

I'm building a Java Web Application using Java EE 6 and JSF-2.0, using the persistence API for all database operations. The back-end is MySQL, but I have used the EntityManager functions and Named Queries in EJB-QL for all operations. Are SQL injection attacks possible in this case? ...

IS DataNucleus GAE JPQL different from JPA1 standard?

Query q = em.createQuery("SELECT u FROM SSUser u WHERE u.emailId=?1") .setParameter(1, email); I thought this would be a valid query, but then I get: No results for query: SELECT FROM SSUser u WHERE u.emailId=?1 What's the right way to express this query? ...

jpql limit number of results

hi, how to limit the number of results from database when I don't care about the other? select e from Entity e /* just ten results, howto? */ ...

use DISTINCT ON Ejb-QL

is it possible to use PostgreSQL-like DISTINCT ON in EJB using named query? o.fromDate,o.empLeaveMasterId,o.employeeInfoId, o.leavePurposeId ,o.toDate,o.createdByUserId,o.createDate,o.lastModifiedUserId,o.lastModifiedDate,o.isSystemRecord The field describe above is my entity bean field and I want to get fromDate wise distinct record ...

How to write JPA query where parameter is a set?

Assuming the following class, how do you find a Person with a particular email address? public class Person implements Comparable<Person> { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="id") private long id = 0; @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fet...

JPQL / HQL fetch join syntax for compatibility with EclipseLink & Hibernate

I would like to be able to swap my JPA implementation between EclipseLink & Hibernate with a simple property change. I can do this ok but what is causing me problems is the named query validation. Using EclipseLink I have to write fetch joins like so: SELECT id FROM IndexDefinition id JOIN FETCH id.index JOIN id.index i JOIN FETCH i....

JAVA: NamedQuery String problem

Hello guys I am having some problems with exact matches while doing a NamedQuery. I am currently using something like this: @NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from Entry e where e.name =:"+ Entry.NAME ) ... Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME); query.setParameter(Entry.NAME...

Correct way to count associated objects using JPQL

What is the correct way to write this JPA query? I am just guessing as I cant work it out or find it in my JPA book. Query query=em.createQuery("select m from Meeting m where count(m.attendees) = 0"); return query.getResultList(); I am currently trying this with Hibernate and I get a mysql error! ERROR org.hibernate.util.JDBCExceptio...