求问,关于卡了我一晚上的hibernate问题。

环境:

jdk1.8.0_92
hibernate-release-5.1.0-Final
jboss插件。

问题描述:
org.hibernate.AnnotationException: No identifier specified for entity: entity.Student

我查了查说是没有主键。可没找到解决方法。

我的代码是:

public class StudentTest {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void init() {
        Configuration configuration = new Configuration().configure();
        sessionFactory = new Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }

    @Test
    public void studentTest() {
        Student student = new Student(1, "zhanhgsa", new Date(), "man");
        session.save(student); // 保存对象进数据库
    }

    @After
    public void destory() {
        transaction.commit();// 提交事务
        session.close();// 关闭会话
        sessionFactory.close();// 关闭会话工厂
    }
}


其映射为:

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-30 22:49:04 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="entity.Student" table="STUDENT">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="assigned" />
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
    </class>
</hibernate-mapping>

hibernate配置

 <?xml version="1.0" encoding="UTF-8"?>
<!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 >
  <property name="hibernate.connection.password">885436</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
  <property name="diolect">org.hibernate.diolect.MySqLDiolect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">create</property>

  <mapping class="entity.Student"/>


 </session-factory>
</hibernate-configuration>

实体类:


@Entity(name="Student")
public class Student {

    private int sid;
    private String sname;
    private String gender;
    private Date birthday;


    public Student(int sid, String sname, Date birthday, String gender) {

        this.sid = sid;
        this.sname = sname;
        this.birthday = birthday;
        this.gender = gender;
    }



    public Student() {
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Date getBirthday() {
        return birthday;
    }

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

    public String getGender() {
        return gender;
    }

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

    @Override
    public String toString() {
        return "Student [sid=" + sid + ", sname=" + sname + ", birthday=" + birthday + ", gender=" + gender + "]";
    }

}


等一下,会不会是你的实体类在sid属性上面没有加@Id注解

映射文件中generator 标签应该加个class吧,主键的生成策略

映射文件的或者中的class应该填入Hibernate能够识别的类型,不要用int,改用integer试试。