Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type

0.0-原本写个test测试myeclipse自动生成的hibernate配置文件,没想到出了以下错误:

Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type for class hibernate1.Hibernate1. Expected: class java.lang.Integer, got class java.lang.String
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:132)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079)
    at org.hibernate.internal.SessionImpl.access$2200(SessionImpl.java:172)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2425)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975)
    at test.MainTest.main(MainTest.java:17)


1.0-测试的代码如下:

package test;
import hibernate1.Hibernate1;//数据库表hibernate1的映射
import org.hibernate.Session;
import com.hibernate.test.HibernateSessionFactory;//自动生成的sessionfactory

public class MainTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Session session = HibernateSessionFactory.getSession();
        Hibernate1 biaoHibernate1=(Hibernate1) session.get(Hibernate1.class, "1");
        System.out.println(biaoHibernate1.getId());
        System.out.println(biaoHibernate1.getUserName());
        HibernateSessionFactory.closeSession();
    }
}

1.1-Hibernate1.java

public class Hibernate1 implements java.io.Serializable {

    // Fields

    private Integer id;
    private String userName;
    private Integer age;

    // Constructors

    /** default constructor D"hljs-keyword">this.id;
    }
    ...略去get&set方法...
    }

1.2-hibernate目录结构图
Exception in thread

1.3-数据表hibernate1
Exception in thread


2.0-分析原因

Object get(Class clazz,Serializable id)

Serializable id中”id”(“1”)的传值无法转化成映射的integer类型

2.1-解决方法

package test;
import hibernate1.Hibernate1;//数据库表hibernate1的映射
import org.hibernate.Session;
import com.hibernate.test.HibernateSessionFactory;//自动生成的sessionfactory

public class MainTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Session session = HibernateSessionFactory.getSession();
        **Hibernate1 biaoHibernate1=(Hibernate1) session.get(Hibernate1.class, new Integer("1"));**
        System.out.println(biaoHibernate1.getId());
        System.out.println(biaoHibernate1.getUserName());
        HibernateSessionFactory.closeSession();
    }

}

强制NEW一个integer类型:
Hibernate1 biaoHibernate1=(Hibernate1) session.get(Hibernate1.class, new Integer(“1”));


3.0-结果输出

四月 25, 2016 2:44:09 上午 org.hibernate.annotations.common.Version 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
四月 25, 2016 2:44:09 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
四月 25, 2016 2:44:09 上午 org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
四月 25, 2016 2:44:09 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
四月 25, 2016 2:44:09 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
四月 25, 2016 2:44:09 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
四月 25, 2016 2:44:09 上午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: hibernate1/Hibernate1.hbm.xml
四月 25, 2016 2:44:09 上午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
四月 25, 2016 2:44:09 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
四月 25, 2016 2:44:09 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO:.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
四月 25, 2016 2:44:09 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
四月 25, 2016 2:44:09 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
四月 25, 2016 2:44:09 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3306/hibernate1]
四月 25, 2016 2:44:09 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root}
四月 25, 2016 2:44:09 上午 org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
四月 25, 2016 2:44:09 上午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
四月 25, 2016 2:44:09 上午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
四月 25, 2016 2:44:09 上午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory 
INFO: HHH000397: Using ASTQueryTranslatorFactory
1
lgl

4.0-感谢&参考
http://bbs.csdn.net/topics/320011020

你可能感兴趣的:(程序开发)