Reputation: 1722
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.
Upvotes: 3
Views: 3022
Reputation: 1513
The are few rules you have to keep in mind when writing Primary key class:
Upvotes: 2