gpt4 book ai didi

java - Tomcat + hibernate = 找不到查询类的持久类

转载 作者:行者123 更新时间:2023-11-28 23:22:14 25 4
gpt4 key购买 nike

我正在开发一个网络应用程序并将其部署在 Tomcat 7.0 上。我的应用程序使用 MySQL 数据库。我已经配置了应用程序和数据库之间的连接,并希望添加对 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。我将所有可用元素添加到 Web 工件中,但没有帮助。我什至尝试使用 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;
}
}

最佳答案

也许这不是我问题的正确答案,但这确实对我有用。我用 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>

获取实体管理器:

public static EntityManager getEntityManager() {
EntityManager entityManager = Persistence
.createEntityManagerFactory("NewPersistenceUnit")
.createEntityManager();

return entityManager;
}

获取所有用户:

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 却不能。如有任何意见/建议,我们将不胜感激。

关于java - Tomcat + hibernate = 找不到查询类的持久类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com