Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Eclipse Struts 2 Hibernate JPA配置_Eclipse_Hibernate_Jpa_Configuration_Struts2 - Fatal编程技术网

Eclipse Struts 2 Hibernate JPA配置

Eclipse Struts 2 Hibernate JPA配置,eclipse,hibernate,jpa,configuration,struts2,Eclipse,Hibernate,Jpa,Configuration,Struts2,我正在使用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应用程序标签中): <?xml version="1.0

我正在使用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应用程序标签中):

<?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>
<?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>
<?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>
因为我使用注释,所以所有配置都很短

persistence.xml(persistence中的所有部分)是:

<?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>
<?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>
<?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>
但输出为
null


为什么?

为了能够注入类,需要对类进行管理,可以是
@Statelss
@Stateful
Servlet
。。。或者是Spring管理的,类似这样的

这是创建
EntityManager所能做的:

EntityManager entityManager = Persistence.createEntityManagerFactory("CRM").createEntityManager();
bean可能不使用注释。您可以从示例中了解更多如何集成框架


您还可以阅读与Spring集成示例的答案

注释@stateful在哪个jar中?我正在使用Hibernate 5.2.14。我已经添加了所有必需的包,但没有添加此批注。此外,如果我使用
EntityManagerFactory emf=Provider.createEntityManagerFactory(“CRM”)
而不是批注方式,它会引发PersistenceException,原因是:没有名为CRM的EntityManager的持久性提供程序。