1

I'm building a webapp using hibernate and wildfly. Below the content of some of my files:

Hibernate model user.java:

package org.declercq.reportbuilderback.models;



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity (name="User")
@Table(name = "users")
public class User {


    @Id @GeneratedValue
    @Column(name="id")
    private int id;

    @Column(name="username")
    private String userName;

    @Column(name="password")
    private String password;

    @Column(name="email")
    private String emailAddress;

    public User(String userName, String password, String emailAddress) {
        super();
        this.userName = userName;
        this.password = password;
        this.emailAddress = emailAddress;
    }

    public User() {
        super();
    }

    public int getId() {
        return id;
    }





}

My hibernate util class:

package org.declercq.reportbuilderback.dao;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
//import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
    private static SessionFactory sessionFactory;



    public static SessionFactory getSessionFactory() {
        if(sessionFactory == null){
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            ServiceRegistry serviceRegistry
                = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);                

        }
        return sessionFactory;
    }
}

My DAO method used:

public List<User> getAllUsers(){
        System.out.println("0");
        Session session=HibernateUtil.getSessionFactory().openSession();
        System.out.println("0,5");
        Transaction tx = null;
        List<User> allUsers=null;
        try{
            tx = session.beginTransaction();
            System.out.println("1");
            allUsers = session.createQuery("FROM org.declercq.reportbuilderback.models.User").getResultList();


            System.out.println("2");

            tx.commit();
            System.out.println("3");
            return allUsers;
        }
        catch(HibernateException e){
            if(tx != null){
                tx.rollback();
                System.out.println("4");
            }
            return allUsers;
        }
        finally{
            session.close();
            System.out.println("5");

        }
    }

This method is called by a web service:

package org.declercq.reportbuilderback.webservices;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.declercq.reportbuilderback.dao.UserDao;
import org.declercq.reportbuilderback.models.User;

@Path("/userwebservice")
public class UserWebService {

    @Path("/users")
    @GET
    @Produces("application/json")
    public List<User> listUsers(){
        System.out.println("HERE");
        List<User>allUsers=new UserDao().getAllUsers();
        System.out.println("Now here");
        System.out.println("Size: "+allUsers.size());
        return allUsers;                
    }

}

My hibernate config file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.password">removed</property>
  <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/reportbuilderwebservices</property>
  <property name="hibernate.connection.username">removed</property>
  <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.use_sql_comments">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.connection.pool_size">20</property>
  <mapping class="org.declercq.reportbuilderback.models.User"/>
  <mapping class="org.declercq.reportbuilderback.models.Permission"/>
  <mapping class="org.declercq.reportbuilderback.models.Role"/>
  <mapping class="org.declercq.reportbuilderback.models.Vulnerability"/>
  <mapping class="org.declercq.reportbuilderback.models.Finding"/>
 </session-factory>
</hibernate-configuration>

When I'm deploying this to wildfly, I'm getting the following in console:

16:47:17,758 INFO  [org.jboss.modules] (main) JBoss Modules version 1.5.2.Final
16:47:18,100 INFO  [org.jboss.msc] (main) JBoss MSC version 1.2.6.Final
16:47:18,239 INFO  [org.jboss.as] (MSC service thread 1-7) WFLYSRV0049: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) starting
16:47:20,325 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0004: Found org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war in deployment directory. To trigger deployment create a file called org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war.dodeploy
16:47:20,488 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http)
16:47:20,532 INFO  [org.xnio] (MSC service thread 1-6) XNIO version 3.4.0.Final
16:47:20,568 INFO  [org.xnio.nio] (MSC service thread 1-6) XNIO NIO Implementation Version 3.4.0.Final
16:47:20,657 INFO  [org.jboss.remoting] (MSC service thread 1-6) JBoss Remoting version 4.0.21.Final
16:47:20,748 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 38) WFLYCLINF0001: Activating Infinispan subsystem.
16:47:20,782 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 37) WFLYIO001: Worker 'default' has auto-configured to 8 core threads with 64 task threads based on your 4 available processors
16:47:20,881 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 46) WFLYNAM0001: Activating Naming Subsystem
16:47:20,896 WARN  [org.jboss.as.txn] (ServerService Thread Pool -- 54) WFLYTX0013: Node identifier property is set to the default value. Please make sure it is unique.
16:47:20,897 INFO  [org.jboss.as.jsf] (ServerService Thread Pool -- 44) WFLYJSF0007: Activated the following JSF Implementations: [main]
16:47:20,916 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
16:47:20,929 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 53) WFLYSEC0002: Activating Security Subsystem
16:47:20,940 INFO  [org.jboss.as.connector] (MSC service thread 1-6) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.3.4.Final)
16:47:20,945 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) WFLYJCA0018: Started Driver service with driver-name = h2
16:47:20,963 INFO  [org.jboss.as.security] (MSC service thread 1-3) WFLYSEC0001: Current PicketBox version=4.9.6.Final
16:47:21,244 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 56) WFLYWS0002: Activating WebServices Extension
16:47:21,318 INFO  [org.jboss.as.naming] (MSC service thread 1-2) WFLYNAM0003: Starting Naming Service
16:47:21,334 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-2) WFLYMAIL0001: Bound mail session [java:jboss/mail/Default]
16:47:21,381 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-7) WFLYUT0003: Undertow 1.4.0.Final starting
16:47:21,904 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0014: Creating file handler for path '/home/wouter/wildfly-10.1.0.Final/welcome-content' with options [directory-listing: 'false', follow-symlink: 'false', case-sensitive: 'true', safe-symlink-paths: '[]']
16:47:22,028 INFO  [org.jboss.as.ejb3] (MSC service thread 1-8) WFLYEJB0482: Strict pool mdb-strict-max-pool is using a max instance size of 16 (per class), which is derived from the number of CPUs on this host.
16:47:22,029 INFO  [org.jboss.as.ejb3] (MSC service thread 1-6) WFLYEJB0481: Strict pool slsb-strict-max-pool is using a max instance size of 64 (per class), which is derived from thread worker pool sizing.
16:47:22,040 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0012: Started server default-server.
16:47:22,042 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0018: Host default-host starting
16:47:22,225 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
16:47:22,744 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
16:47:23,033 WARN  [org.jboss.as.domain.management.security] (MSC service thread 1-4) WFLYDM0111: Keystore /home/wouter/wildfly-10.1.0.Final/standalone/configuration/application.keystore not found, it will be auto generated on first use with a self signed certificate for host localhost
16:47:23,066 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-8) WFLYDS0013: Started FileSystemDeploymentService for directory /home/wouter/wildfly-10.1.0.Final/standalone/deployments
16:47:23,085 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0027: Starting deployment of "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war" (runtime-name: "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war")
16:47:23,635 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-6) ISPN000128: Infinispan version: Infinispan 'Chakra' 8.2.4.Final
16:47:23,683 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443
16:47:23,697 INFO  [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 64) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,722 INFO  [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 64) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,728 INFO  [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 58) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,704 INFO  [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,734 INFO  [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,731 INFO  [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 58) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:24,060 INFO  [org.jboss.ws.common.management] (MSC service thread 1-5) JBWS022052: Starting JBossWS 5.1.5.Final (Apache CXF 3.1.6) 
16:47:26,386 INFO  [org.jboss.weld.deployer] (MSC service thread 1-7) WFLYWELD0003: Processing weld deployment org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war
16:47:26,897 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-7) HV000001: Hibernate Validator 5.2.4.Final
16:47:27,312 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0005: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.4)
16:47:27,364 INFO  [org.jboss.weld.Version] (MSC service thread 1-5) WELD-000900: 2.3.5 (Final)
16:47:27,465 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0018: Started Driver service with driver-name = org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war_org.postgresql.Driver_9_4
16:47:30,321 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 65) RESTEASY002225: Deploying javax.ws.rs.core.Application: class org.declercq.reportbuilderback.webservices.ConfigApp
16:47:30,383 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 65) WFLYUT0021: Registered web context: /org.declercq.reportbuilderback-0.0.1-SNAPSHOT
16:47:30,419 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war" (runtime-name : "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war")
16:47:30,634 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
16:47:30,635 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
16:47:30,637 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 13428ms - Started 476 of 724 services (404 services are lazy, passive or on-demand)
16:51:38,771 INFO  [stdout] (default task-3) HERE
16:51:38,774 INFO  [stdout] (default task-3) 0
16:51:38,891 INFO  [org.hibernate.Version] (default task-3) HHH000412: Hibernate Core {5.2.3.Final}
16:51:38,895 INFO  [org.hibernate.cfg.Environment] (default task-3) HHH000206: hibernate.properties not found
16:51:38,898 INFO  [org.hibernate.cfg.Environment] (default task-3) HHH000021: Bytecode provider name : javassist
16:51:39,501 INFO  [org.hibernate.annotations.common.Version] (default task-3) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
16:51:39,688 WARN  [org.hibernate.orm.connections.pooling] (default task-3) HHH10001002: Using Hibernate built-in connection pool (not for production use!)
16:51:39,690 INFO  [org.hibernate.orm.connections.pooling] (default task-3) HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://127.0.0.1:5432/reportbuilderwebservices]
16:51:39,691 INFO  [org.hibernate.orm.connections.pooling] (default task-3) HHH10001001: Connection properties: {user=reportbuilderwebservices, password=****}
16:51:39,691 INFO  [org.hibernate.orm.connections.pooling] (default task-3) HHH10001003: Autocommit mode: false
16:51:39,695 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (default task-3) HHH000115: Hibernate connection pool size: 20 (min=1)
16:51:39,803 INFO  [org.hibernate.dialect.Dialect] (default task-3) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
16:51:40,086 INFO  [org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl] (default task-3) HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
16:51:40,091 INFO  [org.hibernate.type.BasicTypeRegistry] (default task-3) HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@60f81f2e
16:51:40,453 INFO  [org.hibernate.tool.schema.internal.SchemaCreatorImpl] (default task-3) HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@4788a59c'
16:51:40,642 INFO  [stdout] (default task-3) 0,5
16:51:40,649 INFO  [stdout] (default task-3) 1
16:51:40,681 WARN  [org.hibernate.hql.internal.QuerySplitter] (default task-3) HHH000183: no persistent classes found for query class: FROM org.declercq.reportbuilderback.models.User
16:51:40,684 INFO  [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-3) HHH000397: Using ASTQueryTranslatorFactory
16:51:40,692 INFO  [stdout] (default task-3) 2
16:51:40,693 INFO  [stdout] (default task-3) 3
16:51:40,695 INFO  [stdout] (default task-3) 5
16:51:40,696 INFO  [stdout] (default task-3) Now here
16:51:40,697 INFO  [stdout] (default task-3) Size: 0

The problem is in those last few lines of console output. As you can see in my code (the DAO method and the webservice code), I put some system.out.println statements in between to troubleshoot. All calls are being executed, no errors are thrown. Nevertheless, when I print the size of the resulting list of the database query, it is empty (size 0).

I put some data in that users table and checked manually that the data is there. Nevertheless my code is not retrieving it...

Also, why am I seeing this:

16:51:40,681 WARN  [org.hibernate.hql.internal.QuerySplitter] (default task-3) HHH000183: no persistent classes found for query class: FROM org.declercq.reportbuilderback.models.User

I have a mapping in my hibernate config file, I have the annotations in place in the model class User.java, nevertheless it seems that something is missing? If I replace the query with "From User", then I get an error stating that User is not mapped, but it is in my configuration file? I am forced to use the full package+class name in my HQL query?

Can anyone shed some light on this for me?

Thanks!

aquilares
  • 311
  • 1
  • 6
  • 14

3 Answers3

0

Try

 allUsers = session.createQuery("SELECT u FROM User u").getResultList();
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Sorry mate, no solution. When I do that, I get: Stack Trace org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [SELECT u FROM User u] – aquilares Oct 31 '16 at 16:14
  • And if I use ""SELECT u FROM org.declercq.reportbuilderback.models.User u"" then I get 17:10:12,572 INFO [stdout] (default task-3) 2 17:10:12,575 INFO [stdout] (default task-3) 3 17:10:12,578 INFO [stdout] (default task-3) 5 17:10:12,578 INFO [stdout] (default task-3) Now here 17:10:12,579 INFO [stdout] (default task-3) Size: 0 Again size 0, doesn't fetch the results. – aquilares Oct 31 '16 at 16:19
  • Now that is just funny as it should work. hibernate config is not read propertly. Maybe it is not on classpath – Antoniossss Oct 31 '16 at 16:19
  • My hibernate config is placed (using eclipse, maven project) in root of /src. I already tried, when I place it somewhere else, my app fails with a stacktrace claiming it doesn't find the hibernate config. However, it doesn't complain about that if I keep it where it is, being in the root of /src. So it "seems" like the config file is found. However it complains about mappings... I have no idea what's going on. – aquilares Oct 31 '16 at 16:25
  • And by the way, if you check in the extract of the server logs during deployment, you see that it takes the postgresql connection string, username etc. as specified in the hibernate config file. So the file is found, is deployed and is used. Nevertheless, it's still complaining about the mappings? – aquilares Oct 31 '16 at 16:35
0

Please check this link: Hibernate: no persistent classes found for query class: SELECT p FROM entity.Presentation p

As mentioned name of the entity defaults to the simple class name. It does not contain name of the package.

Community
  • 1
  • 1
Killer
  • 592
  • 1
  • 6
  • 24
  • Ok, so tell me, based on the code and config I placed in my first post, where I'm going wrong? You're saying that it should just work like "From User" right? In the HQL query, you're supposed to use the name of the class, not the name of the tables. Also, check my hibernate config, I have a mapping defined there. Nevertheless, when I use "From User" in my HQL query, I get a stacktrace claiming "User is not mapped". Why is it saying User not mapped, based on my code? Only when I use the full classpath to User.class does the HQL query execute without errors, but then the resulting list is empty. – aquilares Oct 31 '16 at 16:20
  • Strange, it should work. If the error is now "User is not mapped", then there is some problem in the mapping itself. – Killer Oct 31 '16 at 16:35
  • Check this link http://stackoverflow.com/questions/8524767/hibernate-throws-strange-error-class-is-not-mapped – Killer Oct 31 '16 at 16:35
  • yeah but look at the solution of that problem: either it's not listed in my hibernate config (which it is, clearly in my mapped classes), or it's that I'm using the annotations from Hibernate instead of those from javax.persistence, which I'm not. I already hit that stackoverflow post hours ago and double checked that, still not working... – aquilares Oct 31 '16 at 16:38
  • Then do one thing, instead of using HQL, use normal SQL query or criteria query. And then see what is the result. – Killer Oct 31 '16 at 16:40
0

You should use @Entity on User:

@Entity
public class User {
    ...
}

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html

David Mantilla
  • 228
  • 1
  • 10
  • Yeah thanks, and I know, I already googled all day on this problem. I know you're supposed to use the Entity annotation from javax.persistence, not the hibernate one. I already checked and doublechecked that, plus look at my code above, User.java, annotation is there, even specified the entity name, although it's not necessary. – aquilares Oct 31 '16 at 16:23
  • Yes, sorry, there it is ! Is there any chance wildly is not being refreshed with your changes, like it is stuck to an older properties file version ? – David Mantilla Oct 31 '16 at 16:35