Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - IllegalArgumentException: An exception occurred while creating a query in EntityManager:

I have a problem with JPA’s persistence.xml This is the warning that I don't have classes and exception when I try select smth

[EL Warning]: metamodel: 2021-01-26 14:52:53.211-- The collection of metamodel types is empty. Model         
classes may not have been found during entity search for Java SE and some Java EE container managed 
persistence units.  Please verify that your entity classes are referenced in persistence.xml using 
either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> 
element

Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a 
query in EntityManager: 
Exception Description: Problem compiling [select t from Notatka t].

this is my 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="Notatki" transaction-type="RESOURCE_LOCAL">
    
      <class>test.Notatka</class>
        <properties>
          <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
          <property name="javax.persistence.jdbc.url"
                    value="jdbc:mysql://localhost:3306/test" />
          <property name="javax.persistence.jdbc.user" value="root" />
          <property name="javax.persistence.jdbc.password" value="" />
          <!-- EclipseLink should create the database schema automatically -->
          <property name="eclipselink.ddl-generation" value="create-tables" />
          <property name="eclipselink.ddl-generation.output-mode"
                    value="database" />
        </properties>
      </persistence-unit>
    </persistence>

and my main

        public class Main {
        public static void main(String[] args) {
            EntityManagerFactory factory = Persistence.createEntityManagerFactory("Notatki");
            EntityManager em = factory.createEntityManager();
    
    
            Query q = em.createQuery("select t from Notatka t");
            List<Notatka> todoList = q.getResultList();
            for (Notatka todo : todoList)  {
                System.out.println(todo);
            }
            System.out.println("Size: " + todoList.size());
    
         }
        }

I think problem is in this line (but I'm not sure)

EntityManager em = factory.createEntityManager();
question from:https://stackoverflow.com/questions/65902883/illegalargumentexception-an-exception-occurred-while-creating-a-query-in-entity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably you forgot to annotate your test.Notatka class with @Entity, therefore EclipseLink does not add it to the metamodel.

Since you don't have any other classes the resulting metamodel is empty, hence the warning.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...