2017-02-21 5 views
0

新しいMavenプロジェクトを作成し、Eclipse Kepler(Hibernateを含む)にJBossツールをインストールしました。このプログラムを以下のようにテストしたかったのですが、Javaプログラム私はエラー/例外を取得します。ここで私の最初のHibernate JAVAプログラムの修正方法

は、私のファイルは、以下のとおりです。

Structure

ファイルSaveStudent.java:

package com.hibernate.demo; 

import com.hibernate.entity.student; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 


public class SaveStudent { 

    public static void main(String[] args) { 

     SessionFactory f = new Configuration() 
         .configure("hibernate.cfg.xml") 
         .addAnnotatedClass(student.class) 
         .buildSessionFactory(); 

     Session session = f.getCurrentSession(); 

     try { 
      System.out.println("Creating student..."); 
      student st = new student("X","X","[email protected]"); 

      session.getTransaction(); 

      System.out.println("Saving ..."); 
      session.save(st); 

      session.getTransaction().commit(); 

      System.out.println("Done!!!"); 

     } finally { 
      session.close(); 
      f.close(); 
     } 

    } 

} 

ファイルstudent.java:ここ

package com.hibernate.entity; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="hbstudent") 
public class student { 
    @Id 
    @Column(name="id") 
    private int id; 

    @Column(name="first") 
    private String firstname; 

    @Column(name="last") 
    private String lastname; 

    @Column(name="email") 
    private String email; 

public student(){ 
} 

public student(String firstname, String lastname, String email) { 
    this.firstname = firstname; 
    this.lastname = lastname; 
    this.email = email; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getFirstname() { 
    return firstname; 
} 

public void setFirstname(String firstname) { 
    this.firstname = firstname; 
} 

public String getLastname() { 
    return lastname; 
} 

public void setLastname(String lastname) { 
    this.lastname = lastname; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

@Override 
public String toString() { 
    return "student [id=" + id + ", firstname=" + firstname + ", lastname=" 
      + lastname + ", email=" + email + "]"; 
} 



} 

は私のpom.xmlです:

<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>hibernate-tuto</groupId> 
    <artifactId>hibernate-tuto</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 


     <dependencies> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>ejb3-persistence</artifactId> 
     <version>3.3.2.Beta1</version> 
     <type>pom</type> 
    </dependency> 

     <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.6.6</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-cglib-repack</artifactId> 
     <version>2.1_3</version> 
    </dependency> 

    <dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.6</version> 
    </dependency> 
       <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.6.6</version> 
    </dependency> 

    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>1.6.6</version> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.16</version> 
    </dependency> 

<dependency> 
    <groupId>org.ow2.asm</groupId> 
    <artifactId>asm</artifactId> 
    <version>4.0</version> 
</dependency> 
      <dependency> 
    <groupId>cglib</groupId> 
    <artifactId>cglib</artifactId> 
    <version>3.1</version> 
</dependency> 
      <dependency> 
    <groupId>commons-logging</groupId> 
    <artifactId>commons-logging</artifactId> 
    <version>1.1.3</version> 
</dependency> 
<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.6</version> 
</dependency> 

      <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>ejb3-persistence</artifactId> 
    <version>3.3.2.Beta1</version> 
</dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate</artifactId> 
     <version>3.6.0.Beta2</version> 
     <type>pom</type> 
    </dependency> 
    <dependency> 
    <groupId>commons-io</groupId> 
    <artifactId>commons-io</artifactId> 
    <version>2.3</version> 
</dependency> 

      <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>ejb3-persistence</artifactId> 
    <version>3.3.2.Beta1</version> 
</dependency> 

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

<dependency> 
    <groupId>org.hibernate.common</groupId> 
    <artifactId>hibernate-commons-annotations</artifactId> 
    <version>5.0.1.Final</version> 
</dependency> 



<dependency> 
    <groupId>org.hibernate.javax.persistence</groupId> 
    <artifactId>hibernate-jpa-2.1-api</artifactId> 
    <version>1.0.0.Final</version> 
</dependency> 



    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate</artifactId> 
     <version>3.6.0.Beta2</version> 
     <type>pom</type> 
    </dependency> 

    </dependencies> 
</project> 

Hibernate構成ファイル:

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

     <!-- JDBC Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/student?useSSL=false</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 

     <!-- JDBC connection pool settings ... using built-in test pool --> 
     <property name="connection.pool_size">1</property> 

     <!-- Select our SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Echo the SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Set the current session context --> 
     <property name="current_session_context_class">thread</property> 

    </session-factory> 

</hibernate-configuration> 

私が取得エラー:

log4j:WARN No appenders could be found for logger (org.jboss.logging). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;)V 
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.generateDefaultReflectionManager(MetadataBuilderImpl.java:742) 
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:715) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127) 
    at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:654) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) 
    at com.hibernate.demo.SaveStudent.main(SaveStudent.java:16) 
+0

表情で衝突を得ています。 –

+0

http://stackoverflow.com/questions/7685510/log4j-warning-while-initializingによると、単にBasicConfigurator.configure()という行を追加することができます。問題を回避するには –

+1

あなたの休止状態の依存関係には矛盾があります。なぜ、異なるバージョンの非常に多くの同じ依存関係を追加しましたか? –

答えて

0

以下の依存関係を削除します。

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

それはあなたのクラスパスに追加しますLog4j.propertiesファイルを作成するに

<dependency> 
    <groupId>org.hibernate.common</groupId> 
    <artifactId>hibernate-commons-annotations</artifactId> 
    <version>5.0.1.Final</version> 
</dependency> 

click here for more

関連する問題