简体   繁体   中英

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.

My Table, Class and hbm.xml file mapping are as follow.

Table : Table has composite primary key as id and mobile columns.

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 :

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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