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 | 2 years ago
  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 | 2 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 | 2 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;
  3. 0

    GitHub comment 15#9972054

    GitHub | 4 years ago | maxandersen
    javax.ejb.EJBException: java.lang.IllegalArgumentException: Provided id of the wrong type for class com.example.sakila.model.Store. Expected: class java.lang.Byte, got class java.lang.Long
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    GitHub comment 15#9972054

    GitHub | 4 years ago | maxandersen
    java.lang.IllegalArgumentException: Provided id of the wrong type for class com.example.sakila.model.Store. Expected: class java.lang.Byte, got class java.lang.Long: javax.ejb.EJBException: java.lang.IllegalArgumentException: Provided id of the wrong type for class com.example.sakila.model.Store. Expected: class java.lang.Byte, got class java.lang.Long
  6. 0

    DAOAuthentication provider expected Long, got String

    Stack Overflow | 2 years ago | We are Borg
    org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.WirTauschen.model.User. Expected: class java.lang.Long, got class java.lang.String

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    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