我正在为类AccountMessage创建一个组合密钥,该组合密钥是accountId和messageId。我的代码如下
@Embeddable
public class AccountMessagePK implements Cloneable, Serializable {
/** Serial Version UID. */
private static final long serialVersionUID = -1L;
@ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY )
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Basic( optional = false )
@JoinColumn(name = "accountId", nullable = false , insertable = false, updatable = false )
private Account account;
@ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY )
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Basic( optional = false )
@JoinColumn(name = "messageId", nullable = false , insertable = false, updatable = false )
private Message message;
@Basic( optional = false )
@Column( name = "accountId", nullable = false )
public Account getAccount() {
return this.account;
}
public void setAccount(final Account account) {
this.account = account;
}
@Basic( optional = false )
@Column( name = "messageId", nullable = false )
public Message getMessage() {
return this.message;
}
public void setMessage(final Message message) {
this.message = message;
}
}
帐户实体就像
@Entity
@Table(name = "Account")
public class Account implements Cloneable, Serializable {
/** Serial Version UID. */
private static final long serialVersionUID = -1L;
private Integer id = 0;
private String externalId;
.....
public Account() {
}
public Account(Integer id) {
this.id = id;
}
public Account(Integer id, String externalId, Boolean isDeleted) {
this.id = id;
this.externalId = externalId;
this.isDeleted = isDeleted;
}
@Basic( optional = true )
@Column( name = "externalId", length = 40 )
public String getExternalId() {
return this.externalId;
}
public void setExternalId(final String externalId) {
this.externalId = externalId;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic( optional = false )
@Column( name = "accountId", nullable = false )
public Integer getId() {
return this.id;
}
public void setId(final Integer id) {
this.id = id;
}
......
}
但是,当我尝试使用Hibernate条件按如下所述获取AccountMessage时,
Criteria crit = sess.createCriteria(AccountMessage.class);
crit.add(Restrictions.eq("id.account.externalId", filterValue));
crit.list();
我遇到错误
org.hibernate.QueryException: could not resolve property: id.account.externalId of: package.path.AccountMessage
我在网上搜索了Google,发现有人遇到相同的错误,但该解决方案似乎不适用于我的案子。 任何想法?预先感谢。