Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity manager doesn't have method getCriteriaBuilder()

I am introducing JPA2.0 in my application working on Oracle9i database and I added the libraries EclipseLink(JPA2.0) and created the entity classes but when I use

javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();

I get the following error

cannot find symbol symbol : method getCriteriaBuilder() location: interface javax.persistence.EntityManager

my web.xml is version 2.4 and here's my persistence.xml

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="MiraclinPU" transaction-type="JTA">
    <jta-data-source>jdbc/Miraclin</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
   </persistence-unit>
</persistence>

It looks like the app is using JPA1.0 as I read on the forums...Can anyone help?

like image 345
Questionmark Avatar asked Apr 24 '12 13:04

Questionmark


People also ask

Which method of the JPA EntityManager would you use?

persistence package is used to provide an entity manager. Persistence - The Persistence is a bootstrap class which is used to obtain an EntityManagerFactory interface. createEntityManagerFactory() method - The role of this method is to create and return an EntityManagerFactory for the named persistence unit.

What will happen when the clear () of an EntityManager class is invoked?

clear. Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.

What is EntityManagerFactory JPA?

In JPA, the EntityManager interface is used to allow applications to manage and search for entities in the relational database. The EntityManager is an API that manages the lifecycle of entity instances. An EntityManager object manages a set of entities that are defined by a persistence unit.


2 Answers

Use:

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>

And not:

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
like image 75
Thys Andries Michels Avatar answered Nov 07 '22 09:11

Thys Andries Michels


Search in all libraries that are included in your project, and remove the ones containing persistence.xml except the one you need. Then reinclude them again.

like image 35
Questionmark Avatar answered Nov 07 '22 08:11

Questionmark