找不到NamedQuery

时间:2017-10-06 10:13:01

标签: java mysql jpa netbeans eclipselink

我正试图通过JPA与mysql db连接,但它现在让我发疯了。我不知道我是怎么做到的,但现在当我再说一遍,我得到了这个例外:

[EL Warning]: metamodel: 2017-10-06 12:04:17.461--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
Hello world
Exception in thread "main" java.lang.IllegalArgumentException: NamedQuery of name: Vechicle.findAll not found.
at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:351)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1124)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:28)

这是我的文件:

Persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="JavaApplication3PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>javaapplication3.Bus</class>
    <class>javaapplication3.ProductsList</class>
    <class>javaapplication3.Worker</class>
    <class>javaapplication3.Division</class>
    <class>javaapplication3.VechicleType</class>
    <class>javaapplication3.Kontrahent</class>
    <class>javaapplication3.Vies</class>
    <class>javaapplication3.Templ</class>
    <class>javaapplication3.DocumentsList</class>
    <class>javaapplication3.Vechicle</class>
    <properties>
          <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dem?zeroDateTimeBehavior=convertToNull"/>
          <property name="javax.persistence.jdbc.user" value="test"/>
          <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
          <property name="javax.persistence.jdbc.password" value="test"/>
    </properties>
</persistence-unit>
</persistence>

Vechicle.class

@Entity
@Table(name = "vechicle")
@NamedQueries({
    @NamedQuery(name = "Vechicle.findAll", query = "SELECT v FROM Vechicle v"),
    @NamedQuery(name = "Vechicle.findById", query = "SELECT v FROM Vechicle v WHERE v.id = :id"),
    @NamedQuery(name = "Vechicle.findByNrReg", query = "SELECT v FROM Vechicle v WHERE v.nrReg = :nrReg"),
    @NamedQuery(name = "Vechicle.findBySize", query = "SELECT v FROM Vechicle v WHERE v.size = :size")})
public class Vechicle implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    ......
}

Application.class

private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication3PU");
private static EntityManager em = emf.createEntityManager();

public static void main(String[] args) {
    System.out.println("Hello world");
    em.getTransaction().begin();
    List<Vechicle> vech = em.createNamedQuery("Vechicle.findAll").getResultList();
    em.getTransaction().commit();
}

对不起那个烂摊子,但这是我第六次尝试解决它,但我不知道什么是wrog。具有相同配置的其他应用程序工作得非常好...... 有人知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

几分钟之后,我已经从这两个项目中查找了所有属性文件,并且...使用Java9启动的项目现在不适用于JPA。 因此技术更新并不总是好的。

因此,如果有人创建Java 9项目并面对该问题,那么应该转到project.properties并更改linse:

javac.source=9
javac.target=9

为:

javac.source=1.8
javac.target=1.8

Soooo就是这样:))