Search code examples
javamysqljpanetbeanseclipselink

NamedQuery not found


I'm trying to get connection with mysql db by JPA but it's driving me crazy right now. I dunno how I've done it but now when I repeat that I'm getting this Exception:

[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)

Here's my files:

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();
}

Sorry for that mess but it's my sixth attempt to figure it out but I've got no idea what is wrog. Other app with the same configuration is working really well... Have somebody got any ideas what I've done wrong?


Solution

  • After couple of minutes I've ckeched all properties files from both projects, and... Project started with Java9 doesn't work with JPA right now. So technology update are not always good.

    So if somebody create Java 9 project and facing with that problem too should go to project.properties and change linse:

    javac.source=9
    javac.target=9
    

    to:

    javac.source=1.8
    javac.target=1.8
    

    Soooo that's all :)