2017-04-02 50 views
-1

我是新來的冬眠和我研究。當我想開始我的JUnit時,每次都會發生這個錯誤。我猜我的hbm.xml文件有問題。也許我錯過了一些東西,因爲我仍然是新的休眠。session.save()與「未知實體」有一些錯誤

這是我的hbm.xml文件。

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

    <class name="com.test.UserEntity" table="user" schema="" catalog="junwa"> 
     <id name="id" column="id"/> 
     <property name="username" column="username"/> 
     <property name="gender" column="gender"/> 
     <property name="birthday" column="birthday"/> 
     <property name="addres" column="addres"/> 
    </class> 
</hibernate-mapping> 

這是我UserEntity.java文件

package com.test; 
import javax.persistence.*; 
import java.sql.Timestamp; 

@Entity 
@Table(name = "user", schema = "", catalog = "junwa") 
public class UserEntity { 
    private int id; 
    private String username; 
    private String gender; 
    private Timestamp birthday; 
    private String addres; 

    @Id 
    @Column(name = "id") 
    public int getId() { 
     return id; 
    } 

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

    @Basic 
    @Column(name = "username") 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @Basic 
    @Column(name = "gender") 
    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    @Basic 
    @Column(name = "birthday") 
    public Timestamp getBirthday() { 
     return birthday; 
    } 

    public void setBirthday(Timestamp birthday) { 
     this.birthday = birthday; 
    } 

    @Basic 
    @Column(name = "addres") 
    public String getAddres() { 
     return addres; 
    } 

    public void setAddres(String addres) { 
     this.addres = addres; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     UserEntity that = (UserEntity) o; 

     if (id != that.id) return false; 
     if (username != null ? !username.equals(that.username) : that.username != null) return false; 
     if (gender != null ? !gender.equals(that.gender) : that.gender != null) return false; 
     if (birthday != null ? !birthday.equals(that.birthday) : that.birthday != null) return false; 
     if (addres != null ? !addres.equals(that.addres) : that.addres != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = id; 
     result = 31 * result + (username != null ? username.hashCode() : 0); 
     result = 31 * result + (gender != null ? gender.hashCode() : 0); 
     result = 31 * result + (birthday != null ? birthday.hashCode() : 0); 
     result = 31 * result + (addres != null ? addres.hashCode() : 0); 
     return result; 
    } 
} 

這是我的測試文件。

/** 
* Created by junwa on 2017/4/2. 
*/ 
import com.test.Students; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

import java.util.Date; 


public class StudentsTest { 
    private SessionFactory sessionFactory; 
    private Session session; 
    private Transaction transaction; 
    @Before 
    public void init(){ 
     // create a deploy object 
     Configuration config = new Configuration().configure(); 
     // create a service licenced object 
     ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 
     // create a session factory object 
     sessionFactory = config.buildSessionFactory(serviceRegistry); 
     // create a sessoin object 
     session = sessionFactory.openSession(); 
     // start transaction 
     transaction = session.beginTransaction(); 

    } 

    @After 
    public void destroy(){ 
     // commit transaction 
     transaction.commit(); 
     // close session 
     session.close(); 
     // close session factory 
     sessionFactory.close(); 

    } 

    @Test 
    public void testSaveStudents(){ 
     // create a object 
     Students s = new Students(1,"junwa","male",new Date(),"Anhui"); 
     // save object to mysql database 
     session.save(s); 
     session.flush(); 


    } 
} 

這是我的輸出 enter image description here

+0

你有什麼問題?你可以發佈輸出嗎? – PrestonM

+0

謝謝你的幫助。我已經上傳了一張圖片,並且這個。 –

+0

@JunwaDirk當你已經完成了xml文件中的映射時,爲什麼你需要註解?或者反之亦然。 –

回答

0

正如法拉茲杜蘭尼說,當你已經做過的hbm.xml文件的映射,爲什麼你需要註解?反之亦然。你必須刪除其中一個。我會說刪除hbm.xml文件並僅使用註釋。

還有一件事我注意到你是還沒有關閉橫斷也。

+0

如果您發現此(或任何)答案有幫助,請立即加入。如果這回答了您的問題,請將其標記爲已接受的答案。謝謝! –

+0

是啊!我知道了。非常感謝! –

0

您不能同時使用hbm.xml和註釋。

相關問題