0

I'm making a Restful web service using netbeans, tomcat, jpa and jax-rs. I have this error since I've add the <class /> tags to my persistence.xml for all of my classes (And I need them to make a select).

The error is : https://gist.github.com/anonymous/bb37c28cdb3dbdf721c5206bfa6369c3

And my persistence.xml is :

<?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="NataRestServicePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>model.Media</class>
    <class>model.MediaTypeDB</class>
    <class>model.Message</class>
    <class>model.Observation</class>
    <class>model.Session</class>
    <class>model.Species</class>
    <class>model.User</class>
    <class>model.UserType</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/natagora?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="password"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
    </properties>
  </persistence-unit>
</persistence>

And this is the main dao (that worked before, didn't change anything) but without all the CRUD methods (just the read for example)

package dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

public class MainDAO<T> implements IMainDAO<T> {

    protected Class<T> clazz;

    private final EntityManagerFactory factory;
    private static final String PERSISTENCE_UNIT_NAME = "NataRestServicePU";

    @PersistenceContext(unitName = "NataRestServicePU")
    protected EntityManager entityManager;

    public MainDAO(Class<T> type){
        clazz = type;
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);  
    }

    public void setClazz(Class<T> clazzToSet) {
        this.clazz = clazzToSet;
    }

    @Override
    public T read(int id){
        try{
            newEntityManager();
            return entityManager.find(clazz,id);
        }finally{
            closeEntityManager();
        }      
    }

    protected void closeEntityManager(){
        entityManager.close();
    }

    protected void newEntityManager(){
        entityManager = factory.createEntityManager();
    }
}
pirho
  • 11,565
  • 12
  • 43
  • 70
MBek
  • 155
  • 10
  • There is `java.net.ConnectException: Connection refused: connect`? So can you connect? – pirho Nov 21 '17 at 08:31
  • Yes I can, using the same port with MySQL Workbench for example... I don't know why adding the classes to my persistence.xml would change anything that's the problem :/... – MBek Nov 21 '17 at 09:20
  • And you use port 3307 as in persistence.xml (so not default 3306) ? What means _since I've add the "" tags_? – pirho Nov 21 '17 at 09:24
  • Yes I'm using the port 3307, and yes i forgot to type "", I needed it to make a "select"... – MBek Nov 21 '17 at 09:28
  • And If you now remove class tags the exception disappears? – pirho Nov 21 '17 at 09:32
  • Yes it works without "'"... But I need to add them or I have this exception for some select : java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: observation is not mapped [from model.Session where session_id = (select session_id from observation where observation_id = (select observation_id from media where media_type_id = 1 and containsBird is false))] – MBek Nov 21 '17 at 09:50
  • Eeeh... I tried to re-add the "" tags and I works but I still have the exception that says that one of my class is not mapped... – MBek Nov 21 '17 at 09:53
  • Ok problem solved... I was using the name of the table and not the name of the class. – MBek Nov 21 '17 at 10:19

0 Answers0