我正在开发一个Web应用程序并在Tomcat 7.0上部署它。我的应用程序使用MySQL数据库。我已经在app和database之间配置了连接,并希望添加Hibernate 5.2.5支持。我可以通过Hibernate控制台与数据库进行通信,使用下面的配置,在非网络应用程序中使用时可以正常工作。问题出在我将其部署在服务器上时。我收到警告
no persistent classes found for query class: from entities.UserH
然后由它引起500服务器错误。
我的实体类:
@Entity
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class UserH {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true, length = 11)
private int id;
@Column(name = "login", nullable = false, length = 45)
private String login;
@Column(name = "name", nullable = false, length = 45)
private String name;
@Column(name = "surname", nullable = false, length = 45)
private String surname;
/* getters and setters*/
}
我的hibernate-annotation.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection properties - Driver, URL, user, password -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/web-database</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Mapping with model class containing annotations -->
<mapping class="entities.UserH"/>
</session-factory>
</hibernate-configuration>
应该获得用户的方法:
public List<UserH> getAllUsers() {
try (Session session = HibernateUtils.getSessionAnnotationFactory().getCurrentSession()) {
session.beginTransaction();
Query<UserH> usersQuery = session.createQuery("from entities.UserH", UserH.class);
List<UserH> usersList = usersQuery.getResultList();
session.getTransaction().commit();
return usersList;
}
}
正如我所提到的 - 它适用于普通的应用程序,但不适用于Tomcat。我将所有可用元素添加到网络工件中,但它没有帮助。我甚至试图用persistance.xml
添加JPA支持,但仍然没有运气。
还有什么问题?
编辑:我的HibernateUtils类:
public class HibernateUtils {
private static SessionFactory sessionAnnotationFactory;
private static SessionFactory buildSessionAnnotationFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate-annotation.cfg.xml");
System.out.println("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
System.out.println("Hibernate Annotation serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionAnnotationFactory() {
if (sessionAnnotationFactory == null)
sessionAnnotationFactory = buildSessionAnnotationFactory();
return sessionAnnotationFactory;
}
}
答案 0 :(得分:0)
您应该只更改查询以仅引用类名:
session.createQuery( "FROM UserH", UserH.class )
当您将实体定义为内部类时,包唯一重要的时间。
对于这些情况,您需要使用完整的班级名称com.c.SomeOutterClass$MyEntity
。另一种方法是更改@Entity
注释,使其包含name属性,以便明确命名实体类:
public class SomeOutterClass {
@Entity(name = "MyEntity")
public static class MyEntity {
}
}
答案 1 :(得分:0)
也许这不是我的问题的正确答案,但这实际上对我有用。我用JPA配置+ hibernate替换了hibernate。
所以不使用hibernate-annotation.cfg.xml + Session而是使用persistance.xml + EntityManager:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>entities.UserH</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/web-database"/>
<property name="javax.persistence.jdbc.user" value="Admin"/>
<property name="javax.persistence.jdbc.password" value="password2@"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
getEntityManager:
public static EntityManager getEntityManager() {
EntityManager entityManager = Persistence
.createEntityManagerFactory("NewPersistenceUnit")
.createEntityManager();
return entityManager;
}
getAllUsers:
public List<UserH> getAllUsers() {
EntityManager entityManager = HibernateUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
TypedQuery<UserH> usersQuery = entityManager.createQuery("from UserH ", UserH.class);
List<UserH> usersList = usersQuery.getResultList();
transaction.commit();
entityManager.close();
return usersList;
}
然而,我仍然不明白为什么这种配置有效,而单独的hibernate没有。任何意见/建议将不胜感激。