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.