我有一个实体类,它使用它的两个字段作为主键(一起)。这是我正在尝试的。我已经有了数据库,所以我不能停止遵循这条路径并创建一个主键。
@Entity
public class Account {
private String description;
private AccountPk id;
public Account (String description) {
this.description = description;
}
protected Account() {
}
@EmbeddedId
public AccountPk getId() {
return this.id;
}
public String getDescription() {
return this.description;
}
public void setId(AccountPk id) {
this.id = id;
}
public void setDescription(String description) {
this.description = description;
}
}
然后我有另一个支持类
public class AccountPk {
private String code;
private Integer number;
public AccountPk() {
}
public String getCode() {
return this.code;
}
public Integer getNumber() {
return this.number;
}
public void setNumber(Integer number) {
this.number = number;
}
public void setCode(String code) {
this.code = code;
}
public int hashCode() {
int hashCode = 0;
if( code != null ) hashCode ^= code.hashCode();
if( number != null ) hashCode ^= number.hashCode();
return hashCode;
}
public boolean equals(Object obj) {
if( !(obj instanceof AccountPk) ) return false;
AccountPk target = (AccountPk)obj;
return ((this.code == null) ?
(target.code == null) :
this.code.equals(target.code))
&& ((this.number == null) ?
(target.number == null) :
this.number.equals(target.number));
}
}
我遇到的问题是映射器类Account.hbm.xml看起来像
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="hello.Account"
table="MESSAGES">
<id
name="id"
column="MESSAGE_ID">
<generator class="increment"/>
</id>
<property
name="description"
column="DESCRIPTION"/>
</class>
</hibernate-mapping>
我确信这个 xml 文件是罪魁祸首,但我不知道如何以正确的方式进行操作。所以,我会感谢你在这方面的帮助。