Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java Hibernate Error: Provided id of the wrong type for class example4.heroesMap. Expected: class java.lang.Integer, got class java.lang.String

I have a table

create table heroes
(
    id       int          null,
    name     varchar(255) null,
    level    int          null,
    ultimate varchar(255) null
);

Then connected cfg.xml file

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/xxi?useSSL=false</property>
        <property name="connection.username">admin</property>
        <property name="connection.password">12345</property>
        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">10</property>
        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">validate</property>

        <mapping class="example4.heroesMap"></mapping>
    </session-factory>
</hibernate-configuration>

And class for my DB

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package example4;

import org.hibernate.annotations.GeneratorType;

import javax.persistence.*;

@Entity
@Table(name = "heroes")
public class heroesMap {
    @Id
//    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "level")
    private int level;
    @Column(name = "ultimate")
    private String ultimate;
}

Error says that type mismatch in table definition but it looks similar, what have I done wrong?
Error text: Provided id of the wrong type for class example4.heroesMap. Expected: class java.lang.Integer, got class java.lang.String

I wanted to read record from the table but it keeps crashing

public class Hibernate {
    Session session;
    Hibernate(){
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                                                .configure("hibernate.cfg.xml").build();
        Metadata meta = new MetadataSources(registry).getMetadataBuilder().build();
        SessionFactory factory = meta.getSessionFactoryBuilder().build();
        this.session = factory.openSession();
    }
    public String getRecord(String id){
        return this.session.get(heroesMap.class,id).toString();
    }
    public void close(){
        this.session.close();
    }
}

>Solution :

In your heroesMap class, you have defined the id field as an int:

private int id;

However, in your getRecord method, you’re passing a String as the ID:

public String getRecord(String id){
    return this.session.get(heroesMap.class,id).toString();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading