我是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);
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>
答案 0 :(得分:8)
您需要将addScalar();
放入本机sqlquery语法中。为了避免使用ResultSetMetadata的开销,或者只是在返回的内容中更明确,可以使用addScalar()
。
像这样:
SQLQuery query = session.createSQLQuery(sql)
.addScalar("principle", new FloatType())
.addScalar("interest", new FloatType());
答案 1 :(得分:1)
您的数据库使用REAL
数据类型,但您的方言不理解它,因此无法将其映射到其中一种Java类型。请参阅Types.java。