初始SessionFactory创建失败.java.lang.IncompatibleClassChangeError ExceptionInInitializerError

时间:2016-09-27 05:26:07

标签: java hibernate maven

您好我得到以下异常

  

"初始SessionFactory创建   failed.java.lang.IncompatibleClassChangeError:实现类   线程" main"中的例外情况java.lang.ExceptionInInitializerError"

你可以帮我解决这个问题吗?

//Initial SessionFactory creation failed.java.lang.IncompatibleClassChangeError: Implementing class
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.ihub.Hidernate_annotations.persistance.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
    at com.ihub.Hidernate_annotations.persistance.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.ihub.Hidernate_annotations.App.main(App.java:17)
Caused by: java.lang.IncompatibleClassChangeError: Implementing class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.ihub.Hidernate_annotations.persistance.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more

以下是我的代码:

Student.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;    

@Entity
@Table(name = "Test", catalog = "Demo", uniqueConstraints = {
        @UniqueConstraint(columnNames = "ID")
         })
public class Student implements java.io.Serializable {    

    private static final long serialVersionUID = 1L;
    private Integer stuId;
    private String stuName;
    private String stuDepartment;
    private String stuAddress;

    public Student() {
    }

    public Student(Integer stuId, String stuName,String stuDepartment, String stuAddress) {
        this.stuId=stuId;
        this.stuName = stuName;
        this.stuDepartment=stuDepartment;
        this.stuAddress=stuAddress;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)

    public Integer getStuId() {
        return stuId;
    }

    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    @Column(name = "Name", unique = false, nullable = false, length = 50)
    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name = "Department", unique = false, nullable = false, length = 25)
    public String getStuDepartment() {
        return stuDepartment;
    }

    public void setStuDepartment(String stuDepartment) {
        this.stuDepartment = stuDepartment;
    }

    @Column(name = "Address", unique = false, nullable = false, length = 200)
    public String getStuAddress() {
        return stuAddress;
    }

    public void setStuAddress(String stuAddress) {
        this.stuAddress = stuAddress;
    }       
}

App.java

package com.ihub.Hidernate_annotations;

import org.hibernate.Session;    
import com.ihub.Hidernate_annotations.persistance.HibernateUtil;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
         System.out.println("Maven + Hibernate + MySQL");
         Session session = HibernateUtil.getSessionFactory().openSession();

         session.beginTransaction();
         Student stu = new Student();    

         stu.setStuId(2);
         stu.setStuName("Raghaav");
         stu.setStuDepartment("CSE");
         stu.setStuAddress("GUNTUR");

         session.save(stu);
         session.getTransaction().commit();
    }
}

HibernateUtil.java

package com.ihub.Hidernate_annotations.persistance;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();

        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }    
}

hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping class="com.ihub.Hidernate_annotations.Student"></mapping>
    </session-factory>
</hibernate-configuration>    

以下是依赖

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

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

  <!--    MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>

    <!-- Hibernate core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.2.3.ga</version>
</dependency>

<!-- Hibernate library dependecy start -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
<!--    Hibernate library dependecy end -->

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

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

请帮帮我

2 个答案:

答案 0 :(得分:0)

不推荐使用AnnotationConfigiuration,而是使用配置

import pandas as pd
reader = pd.read_csv("alerts.csv")
print reader[['col1','col3']].loc[reader['col1'] == 1]

答案 1 :(得分:0)

我认为错误是因为你在maven的pom.xml中有不同的hibernate版本。只留下一个休眠版本(最好是最新版本)并重新构建。还要确保所有hibernate依赖项都是相同的版本(Hibernate,hibernate-core等)