我正在使用Guice和Hibernate 5设置应用程序,我可以启动我的应用程序,我可以看到hibernate启动。但是当我尝试用hibernate Session查询数据时,我得到了错误
org.hibernate.UnknownEntityTypeException:无法找到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">
<properties>
</properties>
</persistence-unit>
</persistence>
我的用户实体
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "user")
@Entity(name = "User")
public class User implements Serializable {;
@Id
@Column(unique = true, nullable = false)
private String id;
@Column(unique = true, nullable = false)
private String login;
...
}
道
@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);
System.out.println(session);
User u = session.get(User.class, id);
return u;
}
}
我的测试用例
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://....");
dbProperties.put(AvailableSettings.USER, "");
dbProperties.put(AvailableSettings.PASS, "");
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");
}
并且不知道我缺少什么,我虽然实体将自动扫描,并且不需要persistence.xml中的 xyz..User