Hibernate throws org.hibernate.TypeMismatchException: Provided id of the wrong type for class. Expected: class java.lang.Integer, got class java.lang.Long

Hi,

I’m trying to create a One-To-One relation like this:

@OneToOne
    @JoinColumn(name = "kArtikel", referencedColumnName = "kArtikel")
    public TArtikelSonderpreisEntity getArtikelSonderpreisEntity() {
        return artikelSonderpreisEntity;
    }
 
    public void setArtikelSonderpreisEntity(TArtikelSonderpreisEntity artikelSonderpreisEntity) {
        this.artikelSonderpreisEntity = artikelSonderpreisEntity;
    }

The problem is that referencedColumnName has no effect, because it always uses the primary key of TArtikelSonderpreisEntity (kArtikelSonderpreisEntity). It doesn’t matter If I user referencedColumnName or not. kArtikel is only unique and no foreign or primary key. Can someone tell me what I am doing wrong?

Tables:

create table tArtikelSonderpreis
(
	kArtikelSonderpreis int not null
		primary key,
	kArtikel int,
	nAktiv tinyint,
	dStart datetime,
	nAnzahl int,
	dEnde datetime,
	nIstDatum tinyint,
	nIstAnzahl tinyint
)

create table tartikel
(
	kArtikel bigint not null
		primary key,
	cArtNr varchar(30),
	cName varchar(255),
	cBeschreibung text,
	fVKBrutto float,
	fVKNetto float,
	fUVP money,
	cAnmerkung text,
	cPreisliste char(1),
	cAktiv char(1),
	nLagerbestand real,
	fMwSt real
)

Log:

Feb 22, 2018 10:34:50 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.12.Final}
Feb 22, 2018 10:34:50 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 22, 2018 10:34:50 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Feb 22, 2018 10:34:50 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Feb 22, 2018 10:34:50 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://192.168.192.20\MSSQLSERVER:1433;database=Mandant_Coding]
Feb 22, 2018 10:34:50 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
Feb 22, 2018 10:34:50 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Feb 22, 2018 10:34:50 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Feb 22, 2018 10:34:51 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServer2012Dialect
Feb 22, 2018 10:34:52 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory

Exception I get because of using the wrong column:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class entities.artikel.TArtikelSonderpreisEntity. Expected: class java.lang.Integer, got class java.lang.Long

Regards!

The problem is obvious if you take a look on the database tables and the log message:

  1. In the tArtikelSonderpreis table, the kArtikel column type is INT
  2. In the tartikel table, the kArtikel column type is BIGINT

Hence, the log message:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class entities.artikel.TArtikelSonderpreisEntity. Expected: class java.lang.Integer, got class java.lang.Long

If you want to see how to map a @OneToOne association when the PK is shared, you can check out this article.

1 Like

I edited my post. Thank you!

I changed the type of the column kArtikel in tArtikelSonderpreis to BIGINT. But I’m still getting the same exception. It seems like it is using the primary key of tArtikelSonderpreis (kArtikelSonderpreis) and not the referencedColumn kArtikel.

Maybe you didn’t change it in the entity.