Getting "No query defined for that name" when using entitymanager.
I have the following entity -
#Entity
#Table(name="STUDENT")
#NamedQuery(name="student.getAll", query="select s from Student s")
public class Student {
// ...........
}
And using the Entity Manager as shown below -
public class Student_NamedQuery {
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.udemy.jpa");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Query query = entityManager.createNamedQuery("student.getALL");
List<Student> studentList = query.getResultList();
System.out.println(studentList);
entityManager.close();
entityManagerFactory.close();
}
}
Below is the persistence.xml -
<persistence version="1.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_1_0.xsd">
<persistence-unit name="com.udemy.jpa" transaction-type="RESOURCE_LOCAL">
<class>com.udemy.jpa.Student</class>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/udemy?serverTimezone=UTC" />
<property name="hibernate.connection.user" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
When I run Student_NamedQuery.java, I am getting "No query defined for that name" error. I searched all over the internet but failed to find out the problem.
You seem to have a typing error here. You have not used the correct name of the named query (LL != ll).
Related
In persistence.xml I am creating a database (in-memory database for test) like this:
<persistence-unit name="TestDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jar-file>pathToMyJar.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
That works as expected but is slow. So I saved a sql-script for schema generation like this:
<persistence-unit name="TestDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jar-file>pathToMyJar.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="pathToSchemaGeneratonScript.sql"/>
</properties>
</persistence-unit>
And then tried to use it like this:
<persistence-unit name="TestDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.create-script-source" value="pathToSchemaGeneratonScript.sql"/>
</properties>
</persistence-unit>
Since show_sql is set to true I can see that the script runs, e.g "create table xxx (...".
But no tables seems to exist, e.g:
entityManagerFactory = Persistence.createEntityManagerFactory("TestDatabase");
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
Query query = entityManager.createNativeQuery("insert into xxx (someColumns) values (someValues)");
query.executeUpdate();
Will result in "org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Table "xxx" not found".
I have connected manually and verified that the tables are not there. I also applied the generated sql file manually and then the tables are created as expected.
I've added the jars in the Build path
Here are the Persistence Units from xml file
<persistence-unit name="my-persistence-unit">
</persistence-unit>
<persistence-unit name="StudentManagement"
transaction-type="RESOURCE_LOCAL">
<class>com.model.StudentModel</class>
<properties>
<!-- enable warnings for debugging -->
<property name="openjpa.Log"
value="DefaultLevel=TRACE, Runtime=INFO, Tool=INFO, SQL=TRACE" />
<!-- connection properties -->
<property name="javax.persistence.jdbc.url"
value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="javax.persistence.jdbc.user" value="system" />
<property name="javax.persistence.jdbc.password"
value="system" />
<property name="javax.persistence.jdbc.driver"
value="oracle.jdbc.driver.OracleDriver" />
<property
name="javax.persistence.schema-generation.database.action"
value="create" />
</properties>
</persistence-unit>
Here is my Dao Implementation class
EntityManagerFactory entityManagerfactory = Persistence.createEntityManagerFactory("StudentManagement");
EntityManager entityManager = entityManagerfactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(obj);
transaction.commit();
entityManager.close();
entityManagerfactory.close();
Anything left to do as far as I know these were the only thing required to use JPA with Oracle DB
I am trying to setup a REST API using Hibernate as my ORM Mapper. As IDE I am using IntelliJ. The project folder structure looks like this:
projectstructure
I setup the project with Glassfish like this instructions
The REST-API part works fine so I won't bother pasting all of that in here. To test the Hibernate functionality I wrote this class:
public class ArtistRepository {
public static void testHibernate(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("emf");
EntityManager manager = factory.createEntityManager();
Artist green = new Artist();
green.setName("TEST");
manager.getTransaction().begin();
manager.persist(green);
manager.getTransaction().commit();
}
}
The persistence.xml looks like this:
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="emf">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/database?autoReconnect=true" />
<property name="hibernate.connection.username" value="*****" />
<property name="hibernate.connection.password" value="*****" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</properties>
</persistence-unit>
</persistence>
When I request a REST resource using that method I get the following error:
<p>
<b>exception</b>
<pre>javax.servlet.ServletException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named emf</pre>
</p>
<p>
<b>root cause</b>
<pre>javax.persistence.PersistenceException: No Persistence provider for EntityManager named emf</pre>
</p>
<p>
<b>note</b>
<u>The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 5.0 logs.</u>
</p>
So obviousley the persistence.xml is in the wrong directory, but I have no clue where to put it.
I am using hibernates entityManager to delete an object. However the object is not getting deleted even after flushing the entityManager. It is getting returned if I do a find on it using is id.
Following is what my piece of code looks like:
#PersistenceContext(unitName = "registryPU")
private EntityManager em;
public <E> E delete(Class<E> clazz, String id) {
E en=(E) find(clazz, id);
if(en != null){
em.remove(en);
em.flush();
}
en=(E) find(clazz, id);
assert ( en == null);
return en;
}
public <E> E find(Class<E> clazz, String id) {
return em.find(clazz, id);
}
and the persistence.xml is as follows:
<persistence-unit name="registryPU"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.import_files" value="import.sql"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
</properties>
</persistence-unit>
</persistence>
This should ideally work fine, but is throwing java.lang.AssertionError because of the assert statement. Can someone let me know what is wrong in this and how to avoid it ?
Try to use this after remove(), but before flush()
em.getTransaction().commit();
Can't see <jta-data-source> entry in your persistence.xml. Try adding it as below:
<persistence-unit name="registryPU"
transaction-type="JTA">
<jta-data-source>jdbc/sample</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.import_files" value="import.sql"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
</properties>
</persistence-unit>
</persistence>
I'm currently using the JPA specs to query my objects from the database.
Everytime there's a change made by me (my instance of the software), the items would be properly refreshed.
But, if there's another change made by someone else (other instance, or database change), the items are'nt being properly refreshed.
I'm using a simple "find" with "refresh"
Object found = getManager().find(getModelClass(), id);
getManager().refresh(found);
I'm using a DAO Hierarchy, the "getModelClass" returns my #Entity class like
#Override
protected Class<?> getModelClass() {
return ProductCategory.class;
}
And my Manifest / persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="casa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- localhost -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/database" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.jdbc.wrap_result_sets" value="false" />
<property name="hibernate.hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Also each "DAO" have and instance of and EntityManager of it's own.
What I might be doing wrong ?
Appreciate the help!
Seems like my problem was simple.
The refresh transaction to be properly refreshed from the database needs to be commited
getManager().getTransaction().begin();
T found = (T) getManager().find(getModelClass(), id);
getManager().refresh(found);
getManager().getTransaction().commit();