2015-08-20 60 views
3

我試圖建立與PostgreSQL中的休眠連接,但是我收到一個異常,如下:Hibernate異常 - 創建初始SessionFactory失敗.java.lang.NoSuchMethodError:javax.persistence.Table.indexes()[Ljavax/persistence/Index;

Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index; at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:973)

我看到了另一個話題 - (NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index) - 這也解釋了,我可能有兩個不同版本的類路徑中的hibernate-jpa-api。

其實我有,但我固定的問題,現在顯然我的類路徑中只包含一個,如下:

[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> 

休眠類連接

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> 

由於提前。

+0

如果您是否再次運行'mvn'命令並再次生成項目,您是否從'pom.xml'文件中刪除了多餘的jar文件? –

+0

嗨Joshi。感謝您的答覆。是的,我做了,然後我清理並編譯了這個項目。我不知道發生了什麼事。由於它的項目是新的,我會嘗試使用正確的依賴關係創建另一個項目。誰知道..它是技術= D –

回答

0

我用一個新的pom.xml創建了另一個項目,只有一個hibernate-jpa-api依賴項。 現在它工作正常。

+0

聽起來像你沒有正確地清理其他項目,一旦你已經排序的依賴關係。 – JamesB

相關問題