2016-05-01 14 views
3

おそらく、stackoverflowでの明確な答えでよくある質問です。しかし、2時間の検索の後、私は答えを見つけることができませんでした。org.hibernate.MappingException:不明なエンティティ従業員

私は休止状態で新しくmavenを使用しています。私はthis tutorialを使用していますが、これはアノテーションベースであり、いくつかの変更が加えられています。しかし、それは動作していません。私はいくつかのXMLマッピングファイルを使用しました。 さらに、hibernate xml-base用のEmployee.hbm.xmlを追加します。

この行で>>session.persist(e1);それが例外をスロー

Employee.java

import javax.persistence.*; 
import javax.persistence.Entity; 

@Entity 
@Table(name = "employee") 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING) 
@DiscriminatorValue(value="employee") 

public class Employee { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 

    @Column(name = "id") 
    private int id; 

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

    public int getId() { 
     return id; 
    } 

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

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

} 

Main.java

import org.hibernate.*; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 


public class Main { 
    public static void main(String[] args) { 
     Configuration configuration = new Configuration(); 
     configuration.configure("hibernate.cfg.xml"); 
     ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
       configuration.getProperties()).build(); 

     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry); 
     Session session = sessionFactory.openSession(); 

     Transaction t = session.beginTransaction(); 

     Employee e1 = new Employee(); 
     e1.setName("sonoo"); 

     Regular_Employee e2 = new Regular_Employee(); 
     e2.setName("Vivek Kumar"); 
     e2.setSalary(50000); 
     e2.setBonus(5); 

     Contract_Employee e3 = new Contract_Employee(); 
     e3.setName("Arjun Kumar"); 
     e3.setPay_per_hour(1000); 
     e3.setContract_duration("15 hours"); 

     session.persist(e1); 
     session.persist(e2); 
     session.persist(e3); 

     t.commit(); 
     session.close(); 
     System.out.println("success"); 
    } 
} 

Employee.hbm.xml

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="Employee" table="employee" discriminator-value="employee"> 
     <id name="id"> 
      <generator class="increment"></generator> 
     </id> 

     <discriminator column="type" type="string"></discriminator> 
     <property name="name"></property> 

     <subclass name="Regular_Employee" discriminator-value="reg_emp"> 
      <property name="salary"></property> 
      <property name="bonus"></property> 
     </subclass> 

     <subclass name="Contract_Employee" discriminator-value="con_emp"> 
      <property name="pay_per_hour"></property> 
      <property name="contract_duration"></property> 
     </subclass> 

    </class> 

</hibernate-mapping> 

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

<!-- Generated by MyEclipse Hibernate Tools.     --> 
<hibernate-configuration> 

    <session-factory> 
     <property name="hbm2ddl.auto">update</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">123</property> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <mapping resource="Employee.hbm.xml"/> 
     <mapping class="Employee"/> 
     <mapping class="Contract_Employee"/> 
     <mapping class="Regular_Employee"/> 
    </session-factory> 

</hibernate-configuration> 

例外テキスト

Exception in thread "main" org.hibernate.MappingException: Unknown entity: Employee 
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781) 
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1529) 
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:225) 
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:510) 
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:99) 
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) 
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775) 
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748) 
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753) 
    at Main.main(Main.java:36) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
+0

コンフィギュレーションのためにこれを行うにHibernate 5.を設定するには、この方法を使用することはできません注釈を使用しています。 – hamo

+0

私は知っていますが、私はそれを追加することにしました。おそらく私の問題は解決しましたが、それは私を助けることができませんでした –

+0

私はあなたがContract_EmployeeとRegular_Employeeを表すクラスを持っていると仮定しますか?マッピングファイルを緩めます。 – hamo

答えて

4

あなたは二度の設定をしようと、この

Configuration configuration = new Configuration(); 
configuration.configure("hibernate.cfg.xml"); 
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
        configuration.getProperties()).build(); 

SessionFactory sessionFactory = new Configuration().configure() 
    .buildSessionFactory(serviceRegistry); 

での問題。これは必要ありませんです

Configuration configuration = new Configuration(); 
     configuration.configure("hibernate.cfg.xml"); 

あなたはちょうどあなたがあなたの場合は、マッピングファイルを必要としない

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 

How to configure Hibernate 5

関連する問題