简体   繁体   English

保存实体对象时的Hibernate复合主键问题

[英]Hibernate Composite Primary Key Issue while Saving entity Object

I am trying to save entity using hibernate, But somehow not able to see the record in database table, there is no exception is generated. 我试图使用hibernate保存实体,但不知何故无法在数据库表中看到记录,也没有生成异常。

My Table, Class and hbm.xml file mapping are as follow. 我的Table,Class和hbm.xml文件映射如下。

Table : Table has composite primary key as id and mobile columns. 表:表具有复合主键作为id和移动列。

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT ,
  `mobile` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `name` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   PRIMARY KEY (`id`,`mobile`)
);

Classes : 课程:

public class StudentPk implements Serializable {
    private Integer id;
    private String mobile;

    public StudentPk() {
        super();
    }

    public StudentPk(Integer id, String mobile) {
        super();
        this.id = id;
        this.mobile = mobile;
    }


    public Integer getId() {
        return id;
    }

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

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((mobile == null) ? 0 : mobile.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        StudentPk other = (StudentPk) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (mobile == null) {
            if (other.mobile != null)
                return false;
        } else if (!mobile.equals(other.mobile))
            return false;
        return true;
    }


}


class Student{
    private StudentPk pk;
    private String name;

    --getter-setter-

}

Hibernate Mapping : Hibernate映射:

<class name="com.Student" table="student" proxy="com.Student">
   <composite-id name="id" class="com.StudentPk" >
        <key-property name="id" type="java.lang.Integer" column ="id"/>
        <key-property name="mobile" type="string" column ="mobile"  />
    </composite-id>
    <property name="name" column="name" type="string" />
</class>

I have also map this hbm.xml file in hibernate.cfg.xml file too. 我也在hibernate.cfg.xml文件中映射了这个hbm.xml文件。

Please help me for this. 请帮帮我。

The are few rules you have to keep in mind when writing Primary key class: 在编写主键类时,您必须记住几条规则:

  1. The primary key class must be public and must have a public no-arg constructor. 主键类必须是公共的,并且必须具有公共的无参数构造函数。
  2. The primary key class must be serializable. 主键类必须是可序列化的。
  3. The primary key class must define equals and hashCode methods. 主键类必须定义equals和hashCode方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM