org.hibernate.TypeMismatchException: Provided id of the wrong type for class domainmodel.core.accounts.Account. Expected: class domainmodel.core.accounts.AccountId, got class [Ljava.lang.Object;

Hibernate JIRA | Donnchadh O Donnabhain | 3 years ago
tip
Do you know that we can give you better hits? Get more relevant results from Samebug’s stack trace search.
  1. 0

    Given these classes: {code:java} 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; } } {code} {code:java} package domainmodel.core.accounts; public class AccountId { private final int id; protected AccountId(int id) { this.id = id; } public int intValue() { return id; } } {code} and the following mapping {code:xml} <?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> {code} , this method {code:java} public Account findByCode(String shortCode) { ... return (Account)session.byNaturalId(Account.class).using("shortCode", shortCode).load(); } {code} results the following exception {noformat} org.hibernate.TypeMismatchException: Provided id of the wrong type for class domainmodel.core.accounts.Account. Expected: class domainmodel.core.accounts.AccountId, got class [Ljava.lang.Object; at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070) at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551) at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.load(SessionImpl.java:2727) at core.accounts.AccountRepository.findByCode(AccountRepository.java:217) {noformat} 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.

    Hibernate JIRA | 3 years ago | Donnchadh O Donnabhain
    org.hibernate.TypeMismatchException: Provided id of the wrong type for class domainmodel.core.accounts.Account. Expected: class domainmodel.core.accounts.AccountId, got class [Ljava.lang.Object;
  2. 0

    Given these classes: {code:java} 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; } } {code} {code:java} package domainmodel.core.accounts; public class AccountId { private final int id; protected AccountId(int id) { this.id = id; } public int intValue() { return id; } } {code} and the following mapping {code:xml} <?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> {code} , this method {code:java} public Account findByCode(String shortCode) { ... return (Account)session.byNaturalId(Account.class).using("shortCode", shortCode).load(); } {code} results the following exception {noformat} org.hibernate.TypeMismatchException: Provided id of the wrong type for class domainmodel.core.accounts.Account. Expected: class domainmodel.core.accounts.AccountId, got class [Ljava.lang.Object; at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070) at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551) at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.load(SessionImpl.java:2727) at core.accounts.AccountRepository.findByCode(AccountRepository.java:217) {noformat} 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.

    Hibernate JIRA | 3 years ago | Donnchadh O Donnabhain
    org.hibernate.TypeMismatchException: Provided id of the wrong type for class domainmodel.core.accounts.Account. Expected: class domainmodel.core.accounts.AccountId, got class [Ljava.lang.Object;

    Root Cause Analysis

    1. org.hibernate.TypeMismatchException

      Provided id of the wrong type for class domainmodel.core.accounts.Account. Expected: class domainmodel.core.accounts.AccountId, got class [Ljava.lang.Object;

      at org.hibernate.event.internal.DefaultLoadEventListener.onLoad()
    2. Hibernate
      SessionImpl$NaturalIdLoadAccessImpl.load
      1. org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134)
      2. org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
      3. org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
      4. org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551)
      5. org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.load(SessionImpl.java:2727)
      5 frames
    3. core.accounts
      AccountRepository.findByCode
      1. core.accounts.AccountRepository.findByCode(AccountRepository.java:217)
      1 frame