Hibernate异常 - 初始SessionFactory创建失败.java.lang.NoSuchMethodError:javax.persistence.Table.indexes()[Ljavax / persistence / Index;

时间:2015-08-20 12:55:14

标签: java hibernate pom.xml hibernateexception

我试图与Postgresql建立一个hibernate连接,但是我收到了一个异常,如下所示:

  

引起:java.lang.NoSuchMethodError:   javax.persistence.Table.indexes()[Ljavax /持久/索引;在   org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:973)

我看到了另一个主题 - (NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index) - 这解释了我可能在类路径上有两个不同版本的hibernate-jpa-api。

其实我有,但我修复了这个问题,现在显然我的classpath只包含一个,如下所示:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ savedmoney ---
[INFO] com.marques:savedmoney:war:0.0.1-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] +- org.primefaces:primefaces:jar:5.2:compile
[INFO] +- com.sun.faces:jsf-impl:jar:2.2.12:compile
[INFO] +- com.sun.faces:jsf-api:jar:2.2.12:compile
[INFO] +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] +- com.h2database:h2:jar:1.4.187:compile
[INFO] +- org.postgresql:postgresql:jar:9.4-1201-jdbc41:compile
[INFO] \- org.slf4j:slf4j-api:jar:1.7.12:compile

我仍然收到相同的异常,我找不到任何解决方案。 请参阅下面的我的pom.xml,hibernate.cfg和hibernate连接类。 如果有人可以帮助我,我会很感激。

hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/dsavedmoney</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <property name="connection_pool_size">1</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>

        <mapping class="com.br.savedmoney.model.entity.TUser"/>       
    </session-factory>
</hibernate-configuration>

Hibernate类连接

public class HibernateUtil {

    private static SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            return sessionFactory;
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.marques</groupId>
  <artifactId>savedmoney</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>savedmoney Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.2</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.12</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.12</version>
    </dependency>

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

<!--    <dependency> -->
<!--        <groupId>org.hibernate</groupId> -->
<!--        <artifactId>hibernate-annotations</artifactId> -->
<!--        <version>3.5.6-Final</version> -->
<!--    </dependency> -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.11.Final</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>savedmoney</finalName>
  </build>
</project>

提前致谢。

1 个答案:

答案 0 :(得分:0)

我使用新的pom.xml创建了另一个项目,只有一个hibernate-jpa-api依赖项。 现在它正常工作。