|
Given these classes:
package domainmodel.core.accounts;
public class Account {
private AccountId accountId;
private String shortCode;
public Account(AccountId accountId, String shortCode) {
this.accountId = accountId;
this.shortCode = shortCode;
}
public String getShortCode() {
return shortCode;
}
public AccountId getAccountId() {
return accountId;
}
}
package domainmodel.core.accounts;
public class AccountId {
private final int id;
protected AccountId(int id) {
this.id = id;
}
public int intValue() {
return id;
}
}
and the following mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="domainmodel.core.accounts" default-access="field">
<class name="Account" table="accounts" >
<composite-id name="accountId">
<key-property name="id"/>
<generator class="assigned" />
</composite-id>
<natural-id mutable="true">
<property name="shortCode" column="short_code" not-null="true"/>
</natural-id>
</class>
</hibernate-mapping>
, this method
public Account findByCode(String shortCode) {
...
return (Account)session.byNaturalId(Account.class).using("shortCode", shortCode).load();
}
results the following exception
As a workaround I tried to use a CompositeUserType but ran into HHH-8911. For now I'm not using byNaturalId() but I had hoped to use 2nd-level natural-id cache.
|