【问题标题】:Full Hibernate Plugin only working with Hibernate3. Anyway with Hibernate4?完整的 Hibernate 插件仅适用于 Hibernate3。无论如何与Hibernate4?
【发布时间】:2012-09-10 21:23:24
【问题描述】:

为了学习目的,我一直在为我的 CRUD 演示项目使用 struts2-fullhibernatecore-plugin-2.2.2-GA。以下是我得到的错误,并且不同的休眠依赖项试图使其工作。见下文

错误

java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
    com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory.createAndTestSessionFactory(HibernateSessionFactory.java:284)
    com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:227)
    com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory.getNewSession(HibernateSessionFactory.java:155)

我从这个帖子的回答中明白了 Click Here

比我启动不同的 maven 依赖来检查使用插件的休眠版本并发现

不适用于以下版本

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.6.Final</version>
</dependency>

在这个版本之前工作

  <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>

问题:显然不想使用 Hibernate3 那么有没有办法可以将最新的 hibernate4+ 与 struts2-fullhibernatecore-plugin-2.2.2-GA 一起使用?

问题: struts2中hibernate使用最广泛的DI是什么。如果上述插件不起作用,我应该选择 Spring 还是 google juice?除了 viewpattern 之外,没有看到任何其他可行的管理休眠会话的解决方案,但是说它的反模式并且有很多缺点。

【问题讨论】:

  • 正如您链接到的答案所示,Hibernate 4 与 Hibernate 3 有一些 API 不兼容,所以不,Hibernate 4 与您尝试使用的插件版本不兼容。
  • @steven-benitez,谢谢。我应该怎么做?你能回答我最后一个问题吗?希望得到更多反馈。
  • @steven-benitez,我认为 Open Session in View 模式是最常用的方式?
  • Open Session in View 是一个很好的模式。此外,对于 DI,我更喜欢 Google Guice,但对于每个人来说,我更喜欢他自己的。
  • @steven-benitez,您在 Google Guice 上是否有任何代码示例,其中包含用于 DI 的 hibernate?我正在寻找它。你能分享你自己的实现吗?谢谢

标签: hibernate jakarta-ee struts2 struts2-s2hibernate


【解决方案1】:

使用这个配置

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <components>
                        <component>
                            <name>hbm2ddl</name>
                            <implementation>jpaconfiguration</implementation>
                        </component>
                    </components>
                    <componentProperties>
                        <outputfilename>schema.ddl</outputfilename>
                        <create>true</create>
                        <export>false</export>
                        <format>true</format>
                        <drop>true</drop>
                        <jdk5>true</jdk5>
                        <propertyfile>target/test-classes/jdbc.properties</propertyfile>
                        <skip>${skipTests}</skip>
                    </componentProperties>
                </configuration>
                <!--<executions>-->
                <!--<execution>-->
                <!--<phase>process-test-resources</phase>-->
                <!--<goals>-->
                <!--<goal>hbm2ddl</goal>-->
                <!--</goals>-->
                <!--</execution>-->
                <!--</executions>-->
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>${hibernate.maven.plugin.version}</version>
                        <exclusions>
                            <exclusion>
                                <groupId>cglib</groupId>
                                <artifactId>cglib</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>commons-logging</groupId>
                                <artifactId>commons-logging</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>${hibernate.maven.plugin.version}</version>
                        <exclusions>
                            <exclusion>
                                <groupId>cglib</groupId>
                                <artifactId>cglib</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>commons-logging</groupId>
                                <artifactId>commons-logging</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                        <version>4.2.0.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>${jdbc.groupId}</groupId>
                        <artifactId>${jdbc.artifactId}</artifactId>
                        <version>${jdbc.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

并将&lt;hibernate.maven.plugin.version&gt;3.6.10.Final&lt;/hibernate.maven.plugin.version&gt; 添加到您的 pom 属性中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多