session.save()与"未知实体"有一些错误。

时间:2017-04-02 03:49:48

标签: java hibernate

我刚接触休眠,正如我研究的那样。当我想启动我的JUnit时,每次都会出现这个错误。我想我的hbm.xml文件有问题。也许我错过了一些东西,因为我还不熟悉冬眠。

这是我的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

2 个答案:

答案 0 :(得分:0)

正如Faraz Durrani所说,当您已经在hbm.xml文件中完成映射时,为什么需要注释?或反之亦然。你必须删除其中一个。我会说删除hbm.xml文件并仅使用注释。

我还注意到你还没有关闭横断面

答案 1 :(得分:0)

您不能同时使用hbm.xml和注释。

相关问题