2016-09-01 6 views
-1

hibernateのデモのための簡単なプログラムを書くだけです。しかし、私はマッピングの例外を持っています。Hibernateでのマッピングの例外 - 不明なエンティティ

hibernate.cfg.xmlのは、SRC/hibernateProject.com.himal.hibernatePractise.dtoの下にsrcディレクトリの下に

<?xml version='1.0' encoding='utf-8'?> 
<!-- 
    ~ Hibernate, Relational Persistence for Idiomatic Java 
    ~ 
    ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 
    ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
    --> 
<!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> 

     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">1234</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

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

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property> 

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

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create</property> 

     <!-- Names the annotated entity class --> 
     <mapping class="hibernateProject.com.himal.hibernate.HibernateTest"/> 

    </session-factory> 

</hibernate-configuration> 

ですが、私はUserDetailsクラスを作成します。

package hibernateProject.com.himal.hibernatePractise.dto; 

import javax.persistence.Entity; 
import javax.persistence.Id; 

/** 
* Created by Himal Acharya on 2016-09-01. 
*/ 

@Entity 
public class UserDetails { 

    @Id 
    //for primary key 
    private int userId; 
    private String userName; 

    public int getUserId() { 
     return userId; 
    } 

    public void setUserId(int userId) { 
     this.userId = userId; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
} 

そして、私のテストクラスは

package hibernateProject.com.himal.hibernate; 

import hibernateProject.com.himal.hibernatePractise.dto.UserDetails; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 


/** 
* Created by Himal Acharya on 2016-09-01. 
*/ 
public class HibernateTest { 

    public static void main(String[] args) { 
     //Initiate object of userDetails 
     UserDetails user=new UserDetails(); 

     //setting value 
     user.setUserId(1); 
     user.setUserName("Himal"); 

     //get the configuration 

     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
     Session session=sessionFactory.openSession(); 
     session.beginTransaction(); 
     session.save(user); 

     //Ending Transcation 
     session.getTransaction().commit(); 



    } 
} 

あるしかし、私は次のエラーました:cosoleに述べたように

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" org.hibernate.MappingException: Unknown entity: hibernateProject.com.himal.hibernatePractise.dto.UserDetails 

エラーがhibernateProject.com.himal.hibernate.HibernateTest.mainであるがin line session.save(user);あなたが休止状態@org.hibernate.annotations.Entityはとてもこの

import org.hibernate.*; 

によって、あなたの休止状態-config.xmlにJPA

下にあるこの

import javax.persistence.Entity; 
import javax.persistence.Id; 

に代わるものではありませJPAエンティティを使用している

+0

ことができますに変更してみてください –

+0

ええ.Chnaging this worked –

答えて

1

のようなパッケージの命名規則に従う、あなたはエンティティをマッピングする必要が

<mapping class="hibernateProject.com.himal.hibernatePractise.dto.User‌​Details"/> 

を追加することによって、あなたのエンティティクラスをマップ:

<mapping class="hibernateProject.com.himal.hibernatePractise.dto.UserDetails "/>

0

テストの代わりにテストc少女は、あなたはhibernate.cfg.xmlでcom.himal.hibernatePractise.dto.User‌​‌​Details

+0

cha が正常に機能しました。 –

+0

@HimalAcharya確かにそうです –

関連する問題