Hibernate with guice unable to find entities


#1

Hello,
I am very new to hibernate and i wanted to use it in one of my new project.
I am using Guice 4.1 and latest version of Hibernate.
i can start my application and i can see that hibernate start. but when i try to query data with hibernate Session i get the error

org.hibernate.UnknownEntityTypeException: Unable to locate persister: xyz.entity

persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence
   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"
   version="2.1">
   <persistence-unit name="db-manager" transaction-type="RESOURCE_LOCAL">

    <!---
      I dont want to list all entity here, i want to load then at runtime
      or in the application setting
    --->

     <properties>
     </properties>
   </persistence-unit>
 </persistence>

My user entity

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

@Table(name = "user")
@Entity
public class User implements Serializable {

  @Id
  @Column(unique = true, nullable = false)
  private String id;

  @Column(unique = true, nullable = false)
  private String login;
  
// constructor, setter, getter
}

Dao

@Singleton
public class UserDao  {

  private final Provider<EntityManager> em;

  @Inject
  UserDao(Provider<EntityManager> em) {
    this.em = em;
  }

  public User get(String id) {
    Session session = em.get().unwrap(Session.class);

    // Here exception happen
    User u = session.get(User.class, id);

   return u;
  }

}

my test case

public class UserDaoTest {

  private static UserDao userDao;

  @BeforeClass
  public static void setup() {
    Map<String, Object> dbProperties = Maps.newHashMap();
    dbProperties.put(AvailableSettings.JPA_PERSISTENCE_PROVIDER, "org.hibernate.jpa.HibernatePersistenceProvider");
    dbProperties.put(AvailableSettings.SCANNER_DISCOVERY, "class");
    dbProperties.put(AvailableSettings.DRIVER, "com.mysql.cj.jdbc.Driver");
    dbProperties.put(AvailableSettings.URL, "jdbc:mysql://127.0.0.1:3306/zeppelinhub");
    dbProperties.put(AvailableSettings.USER, "zepl");
    dbProperties.put(AvailableSettings.PASS, "secret");
    dbProperties.put(AvailableSettings.DIALECT, "org.hibernate.dialect.MySQLDialect");
    dbProperties.put(AvailableSettings.SHOW_SQL, "true");
    dbProperties.put(AvailableSettings.HBM2DDL_AUTO, "validate");

    dbProperties.put(AvailableSettings.CONNECTION_PROVIDER, "org.hibernate.hikaricp.internal.HikariCPConnectionProvider");
    dbProperties.put("hibernate.hikari.minimumIdle", "5");
    dbProperties.put("hibernate.hikari.maximumPoolSize", "10");
    dbProperties.put("hibernate.hikari.idleTimeout", "30000");



    JpaPersistModule jpaPersistModule = new JpaPersistModule("db-manager").properties(dbProperties);


    Injector injector = Guice.createInjector(jpaPersistModule, // other module -> bind(UserDao.class).asEagerSingleton());

    PersistService persistService = injector.getInstance(PersistService.class);
    persistService.start();

    userDao = injector.getInstance(UserDao.class);

  }

  @Test
  public void test() {
    userDao.get("UXXXXXXX1");
  }

And dont know what i am missing, i though entity will be automatically scan.

thanks you.


#2

You need to add the <exclude-unlisted-classes>false</exclude-unlisted-classes> directive in persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
 <persistence
   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"
   version="2.1">
   <persistence-unit name="db-manager" transaction-type="RESOURCE_LOCAL">

     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

     <exclude-unlisted-classes>false</exclude-unlisted-classes>

     <properties>
     </properties>
   </persistence-unit>
 </persistence>

Also, the provider is better to be added to persistence.xml.

And, about this:

dbProperties.put(AvailableSettings.HBM2DDL_AUTO, "validate");

You need to have all tables created in the database if you want to use only the validate step. If you want to auto-generate the DB schema in your test environement, use update instead.


#3

Hi vlad,

Thank you for your reply, so i updated my persistence.xml as suggested but still no progress

<?xml version="1.0" encoding="UTF-8"?>
<persistence
  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"
  version="2.1">
  <persistence-unit name="db-manager" transaction-type="RESOURCE_LOCAL">

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
    </properties>
  </persistence-unit>
</persistence>

This is the full log

09:31:29,578  INFO org.hibernate.jpa.internal.util.LogHelper:31 - HHH000204: Processing PersistenceUnitInfo [
	name: db-manager
	...]
09:31:29,688  INFO org.hibernate.Version:45 - HHH000412: Hibernate Core {5.2.12.Final}
09:31:29,690  INFO org.hibernate.cfg.Environment:213 - HHH000206: hibernate.properties not found
09:31:29,750  INFO org.hibernate.annotations.common.Version:66 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
09:31:29,881  WARN org.hibernate.orm.connections.pooling:70 - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
09:31:29,882  INFO org.hibernate.orm.connections.pooling:126 - HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:mem:/_db_test;DB_CLOSE_DELAY=-1;mode=MySQL]
09:31:29,883  INFO org.hibernate.orm.connections.pooling:135 - HHH10001001: Connection properties: {user=user, password=****}
09:31:29,883  INFO org.hibernate.orm.connections.pooling:140 - HHH10001003: Autocommit mode: false
09:31:29,886  INFO org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl:41 - HHH000115: Hibernate connection pool size: 20 (min=1)
09:31:29,943  INFO org.hibernate.dialect.Dialect:157 - HHH000400: Using dialect: org.hibernate.dialect.H2Dialect

org.hibernate.UnknownEntityTypeException: Unable to locate persister: com.xxx.model.database.User

Also my project dir look like this

/proj/src/main/java/com/org/service/model/User.java
/proj/src/main/resource/META-INF/persistence.xml

thanks


#4

You can download the ORM JPA test case as explained in this article, and an entity and some data access logic, and see it works like a charm.

Then, you can compare with your example and debug to see why entities are scanned properly in the JPA test case example (which also uses auto-scanning), and it does not work for you.


#5

Thanks Vlad, i will try.