私は、2つのフィールドを主キー(一緒に)として使用するEntityクラスを持っています。ここに私がしようとしているものがあります。私はすでにデータベースを持っていますので、このパスをたどり、単一の主キーを作成するだけではいけません。2つのフィールドを休止状態で一意のIDとして使用する
@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;
}
}
その後、私は私が
<?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>
のように見えたAccount.hbm.xml、私は問題を抱えていることはマッパークラスである
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));
}
}
別の支持クラスを持っていますこのxmlファイルが原因だと確信していますが、正しい方法でこれを行う方法はわかりません。だから、私はあなたの助けに感謝します。このような例のための代わりに<id>
の
私はこの記事を助けることができると思う: http://stackoverflow.com/質問/ 6405746 /マッピング - 複合主キーと注釈を持つmanytomany –