我是hibernate和Java的新手。我正在尝试执行本机sql查询,但是我被卡住了。谁能帮助我或检查我在哪里做错了?
我的Java代码是 :
try { trns = session.beginTransaction(); String sql = "select principle,interest from salaryinfo where empid = " + selectedempid + ""; SQLQuery query = session.createSQLQuery(sql); List<Object[]> rows = query.list(); for (Object[] row : rows) { Salaryinfo si= new Salaryinfo(); si.setPrinciple(Float.parseFloat(row[0].toString())); si.setInterest(Float.parseFloat(row[1].toString())); sis.add(si); }
错误跟踪 :
Severe: org.hibernate.MappingException: No Dialect mapping for JDBC type: 7 at org.hibernate.dialect.TypeNames.get(TypeNames.java:56) at org.hibernate.dialect.TypeNames.get(TypeNames.java:81) at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:192) at org.hibernate.loader.custom.CustomLoader.getHibernateType(CustomLoader.java:161) at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:131) at org.hibernate.loader.Loader.getResultSet(Loader.java:1678) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2145) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029) at org.hibernate.loader.Loader.list(Loader.java:2024) . . . .
我确实调试了程序,发现程序中断了 SQLQuery query = session.createSQLQuery(sql);
SQLQuery query = session.createSQLQuery(sql);
hibernate配置文件:
<hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">****</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/empdata</property> <property name="hibernate.connection.username">****</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="hibernate.show_sql">false</property> <property name="hibernate.format_sql">false</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.c3p0.acquire_increment">1</property> <property name="hibernate.c3p0.idle_test_period">10</property> <!-- seconds --> <mapping resource="hibernate/Salaryinfo.hbm.xml" /> </session-factory> </hibernate-configuration>
您需要输入addScalar();本机sqlquery语法。为了避免使用ResultSetMetadata的开销,或者只是为了更明确地说明返回的内容,可以使用addScalar()。
addScalar();
addScalar()
像这样:
SQLQuery query = session.createSQLQuery(sql) .addScalar("principle", new FloatType()) .addScalar("interest", new FloatType());