【问题标题】:Eclipse Struts 2 Hibernate JPA configurationEclipse Struts 2 Hibernate JPA 配置
【发布时间】:2018-08-12 16:26:47
【问题描述】:

我正在使用 Eclipse 构建一个 Web 应用程序,我想在其中使用框架 Struts 2 和 Hibernate。这次我没有使用 Maven,只是因为我想知道如果我不使用 Maven,如何使它工作。

版本:

  • Struts 2.5
  • 休眠 5.2.14

现在,工作集是:

META-INF 和 WEB-INF 是:

如图所示,persistence.xml 在 META-INF 中,web.xml 文件在 WEB-INF 中。

web.xml(全部在 web-app 标签中):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>LearningStruts</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml(在 DOCTYPE 之后):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.locale" value="zh_CN"></constant>
    <constant name="struts.custom.i18n.resources" value="mess" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
</struts>

因为我使用注解,所以所有的配置都很短。

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="CRM">
        <description>
            Persistence unit for Hibernate User Guide
        </description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" 
                value="jdbc:mysql://localhost:3306/test_db" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

在一个类中,我使用注解注入EntityManagerFactory

public class UserDao {

    @PersistenceUnit(unitName = "CRM")
    private EntityManagerFactory emf;

    public void insert() {
        System.out.println(emf);
    }
}

但输出是null

为什么?

【问题讨论】:

    标签: eclipse hibernate jpa configuration struts2


    【解决方案1】:

    这是你可以做的创建EntityManager

    EntityManager entityManager = Persistence.createEntityManagerFactory("CRM").createEntityManager();
    

    注解可能没有被 bean 使用。您可以从Getting Started 了解更多如何集成框架示例。

    您还可以阅读this 答案,了解与 Spring 集成的示例。

    【讨论】:

      【解决方案2】:

      需要管理类以便能够注入其中,可以是 @Statelss@StatefulServlet ... 或 Spring managed ,类似的东西

      【讨论】:

      • 注解@stateful在哪个jar?我正在使用休眠 5.2.14。我已经添加了所有必需的包,但没有这个注释。
      • 另外,如果我使用EntityManagerFactory emf = Provider.createEntityManagerFactory ("CRM") 而不是注释方式,它会抛出一个 PersistenceException,原因是:没有名为 CRM 的 EntityManager 的持久性提供程序。
      猜你喜欢
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 2015-06-25
      • 2013-04-22
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多