升级到hibernate 5后,Spring 4的hibernate依赖问题

时间:2017-03-13 06:59:12

标签: java spring hibernate maven

下面是我的pom:

<dependencies>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.dvdme</groupId>
        <artifactId>ForecastIOLib</artifactId>
        <version>1.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.wewherego</groupId>
        <artifactId>api-core</artifactId>
        <version>1.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.32</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.2.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.7</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.7</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>10.2.0.4.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.lambdaj</groupId>
        <artifactId>lambdaj</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.98</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-easymock</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

hibernate配置由注释设置:

public class HibnerateConfiguration {

//declarations that are ignored

public static AnnotationConfiguration config() throws IOException {
    AnnotationConfiguration hibernateConfig = new AnnotationConfiguration();
    hibernateConfig.setProperty(HIBERNATE_DIALECT_STRING, HIBERNATE_DIALECT_MYSQL_VALUE);
    hibernateConfig.setProperty(HIBERNATE_DRIVER_CLASS_STRING, HIBERNATE_DRIVER_CLASS_MYSQL_VALUE);
    String jdbcString = "jdbc:"
            + getHost()
            + ":"
            + .getPort()
            + "/"
            + getDBName()
            + "?autoReconnect=true";
    hibernateConfig.setProperty(HIBERNATE_DB_URL_STRING, jdbcString);
    hibernateConfig.setProperty(HIBERNATE_DB_USERNAME_STRING, getUser());
    hibernateConfig.setProperty(HIBERNATE_DRIVER_PW_STRING, getPassword());
    hibernateConfig.setProperty(HIBERNATE_SESSION_CONTEXT_CLASS_STRING, HIBERNATE_SESSION_CONTEXT_CLASS_VALUE);
    hibernateConfig.setProperty(HIBERNATE_TIMEOUT_STRING, HIBERNATE_TIMEOUT_VALUE);
    hibernateConfig.addAnnotatedClass(myClass.class);
    return hibernateConfig;
}

}

处理会话时,我有:

package com.wewherego.api.malloc.infrastructure;

public class SessionHandler {
    private static SessionFactory sessions= null;
    static {
        try {
            if (sessions== null) {
                synchronized (SessionFactory.class) {
                    sessions= HibnerateConfiguration.config().buildSessionFactory();
                }
            }
        }
        catch(Throwable t) {
            throw new ExceptionInInitializerError(t);
        }
    }

    /*
     * Returns a session anyway. If currently no session exist, open a new one;
     * If there is a current session, use the existing one.
     * ***NOTE: potential issue in this method when dealing with session like this***
     */
    @Override
    public Session getSession() {
        try {
            return sessions.getCurrentSession();
        }
        catch (HibernateException he) {
            return sessions.getCurrentSession();
        }
    }

    public void close() {
        sessions.close();
    }
}

它被用作:

session.createQuery(String.format("FROM %s", "myTable"));

将抛出此异常:

Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.createQuery(Ljava/lang/String;)Lorg/hibernate/query/Query;

在运行时,依赖项是:

runtime dependencies

hibernate文档在Session类中仍然有createQuery:https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#createQuery(java.lang.String)

但确定我错过了什么?

2 个答案:

答案 0 :(得分:1)

您的配置中可能缺少以下任何内容

  1. <%= submit_tag "Help", :type => 'button', :onclick => 'alert("If Zipcode is missing in list at left, do: \n\n\ 1. Enter any zipcode and click Create Client. \n\ 2. Goto Zipcodes and create new zip code. \n\ 3. Edit this new client from the client list.\n\ 4. Select the new zipcode ")' %>这将返回Session session = HibnerateConfiguration.config().buildSessionFactory();而不是SessionFactory

  2. Hibernate 5你需要两个jar:hibernate-core和hibernate-entitymanager,我认为hibernate-entitymanager缺失

  3. hibernate.cfg.xml或任何形式的applicationContext都应该位于应用程序的类路径的根目录中

  4. 需要查看配置文件和应用程序的类路径

答案 1 :(得分:1)

你的类路径上有两个版本的hibernate:3.2.6和5.2。删除版本3.2.6

问题部分是由于hibernate项目在版本3.2和3.3之间更改了maven工件名称,使得maven版本冲突处理无效。

要查看项目中提取版本3.2.6的内容,请使用以下命令:

mvn dependency:tree

您将获得依赖关系树。找到拉hibernate.jar的内容,然后使用<excludes>标签排除依赖关系。

从快速查看依赖列表,不需要这种依赖,因为注释直接包含在hibernate-core.jar中(或hibernate-core引入的依赖之一):

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.3.1.GA</version>
</dependency>

它是pulling hibernate 3.2.6.ga。删除它,并重试,它可能是唯一一个拉休眠3.2.6。