小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

No Dialect mapping for JDBC type: 7

 hh3755 2011-04-29
http://topic.csdn.net/u/20090304/20/d466a78e-2b2c-4623-8973-c998dda5612a.html
沒想到用SQLQUERY的時(shí)候可以有個(gè)簡單的東西,就是
我看了錯誤信息有很多關(guān)于list的,網(wǎng)上幫你搜了一下,希望對你有所幫助。

createQuery與createSQLQuery兩者區(qū)別是:  


前者用的hql語句進(jìn)行查詢,后者可以用sql語句查詢  

前者以hibernate生成的Bean為對象裝入list返回  

后者則是以對象數(shù)組進(jìn)行存儲  

所以使用createSQLQuery有時(shí)候也想以hibernate生成的Bean為對象裝入list返回,就不是很方便  

突然發(fā)現(xiàn)createSQLQuery有這樣一個(gè)方法可以直接轉(zhuǎn)換對象  

Query query = session.createSQLQuery(sql).addEntity(XXX.class);  

XXX 代表以hibernate生成的Bean的對象,也就是數(shù)據(jù)表映射出的Bean。  

太牛了啊.我郁悶啊.

No Dialect mapping for JDBC type: 7 
I got this error while I was working with hibernate and SQL Server. Following are the findings I came across. Hope this helps you too rectify this error. 

Following questions are relevant to understand this clearly. 

What is jdbc type 7? 
java.sql.Type.REAL : It is a constant in the java programming language, that identifies SQL type REAL. 
Value of this constant is 7. This is the above mentioned jdbc type 7. 

What is dialect? 
Dialect is the type of the database that hibernate is going to use. Following are the commonly used dialects. These are the subclasses of the org.hibernate.dialect.Dialect for specific databases. 

org.hibernate.dialect.HSQLDialect 
org.hibernate.dialect.Oracle9Dialect 
org.hibernate.dialect.MySQLDialect 
org.hibernate.dialect.SQLServerDialect 
org.hibernate.dialect.FirebirdDialect 
What cause this error to occur? 
The database may contain a table with field of datatype real. Hibernate dialect SQLServerDialect doesnot understand this type. So we need to explicitly convert this real type to one that dialect can understand. One way to achieve this is to write a subclass of org.hibernate.dialect.SQLServerDialect. 


package co.nr.javaalert.hibernate.dialect; 
import java.sql.Types; 
public class SubSQLServerDialect extends org.hibernate.dialect.SQLServerDialect{ 
public SQLServerDialectForBilling() { 
super(); 
registerColumnType(Types.REAL,"number($p,$s)"); 
registerHibernateType(Types.REAL,"double"); 
} 
} 

registerColumnType() method register a type name for a given type code. This step register sql column data type NUMBER(precision,scale) to Types.REAL. Then register this Types.REAL with a data type that can understand hibernate. The recommended Java mapping for the sql REAL type is as a Java float. 

Use this subclass instead of org.hibernate.dialect.SQLServerDialect in hibernate.cfg.xml. 

Reference 
SQL dialects reference/Data structure definition/Data types/Numeric types 

Hibernate 數(shù)據(jù)庫連接及SQLDialect 錯誤: No Dialect mapping for JDBC type

時(shí)間:2010-01-22 14:19 點(diǎn)擊:1235次 字體:[  ]
Hibernate 不同數(shù)據(jù)庫的連接及SQL方言

 

<!--MySql 驅(qū)動程序 eg. mysql-connector-java-5.0.4-bin.jar-->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

  <!-- JDBC URL -->
  <property name="connection.url">jdbc:mysql://localhost/dbname?characterEncoding=gb2312</property>

  <!-- 數(shù)據(jù)庫用戶名-->
  <property name="connection.username">root</property>

  <!-- 數(shù)據(jù)庫密碼-->
  <property name="connection.password">root</property>
  
  
  <!--Sql Server 驅(qū)動程序 eg. jtds-1.2.jar-->
  <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

  <!-- JDBC URL -->
  <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname</property>

  <!-- 數(shù)據(jù)庫用戶名-->
  <property name="connection.username">sa</property>

  <!-- 數(shù)據(jù)庫密碼-->
  <property name="connection.password"></property>

  
  
  <!--Oracle 驅(qū)動程序 ojdbc14.jar-->
  <property name="dialect">org.hibernate.dialect.OracleDialect</property>
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

  <!-- JDBC URL -->
  <property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property>

  <!-- 數(shù)據(jù)庫用戶名-->
  <property name="connection.username">test</property>

  <!-- 數(shù)據(jù)庫密碼-->
  <property name="connection.password">test</property>



如果出現(xiàn)如下錯誤,則可能是Hibernate SQL方言 (hibernate.dialect)設(shè)置不正確。
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]'last_insert_id' 不是可以識別的 函數(shù)名。

RDBMS方言
DB2org.hibernate.dialect.DB2Dialect
DB2 AS/400org.hibernate.dialect.DB2400Dialect
DB2 OS390org.hibernate.dialect.DB2390Dialect
PostgreSQLorg.hibernate.dialect.PostgreSQLDialect
MySQLorg.hibernate.dialect.MySQLDialect
MySQL with InnoDBorg.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAMorg.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version)org.hibernate.dialect.OracleDialect
Oracle 9i/10gorg.hibernate.dialect.Oracle9Dialect
Sybaseorg.hibernate.dialect.SybaseDialect
Sybase Anywhereorg.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Serverorg.hibernate.dialect.SQLServerDialect
SAP DBorg.hibernate.dialect.SAPDBDialect
Informixorg.hibernate.dialect.InformixDialect
HypersonicSQLorg.hibernate.dialect.HSQLDialect
Ingresorg.hibernate.dialect.IngresDialect
Progressorg.hibernate.dialect.ProgressDialect
Mckoi SQLorg.hibernate.dialect.MckoiDialect
Interbaseorg.hibernate.dialect.InterbaseDialect
Pointbaseorg.hibernate.dialect.PointbaseDialect
FrontBaseorg.hibernate.dialect.FrontbaseDialect
Firebirdorg.hibernate.dialect.FirebirdDialect
以上是轉(zhuǎn)載來的
///////////////////////////////////////////////////////////////////////////////////////////
有時(shí)候,hibernate用原生SQL 查詢,native SQL 的時(shí)候,會出現(xiàn)
org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 ,-16,等等的錯誤
type 類型代碼 可以看 java.sql.Types 中的定義,看源碼就可以了

那是因?yàn)?Dialect  未定義,重寫類,把未定義的Dialect 注冊一下即可,
并且在 hibernate.cfg.xml中加入
Xml代碼
  1. <property name="hibernate.dialect">  
  2.     org.hibernate.dialect.OracleCustomDialect   
  3. </property>  


Java代碼
  1. import java.sql.Types;   
  2.   
  3. import org.hibernate.Hibernate;   
  4.   
  5. public class OracleCustomDialect extends org.hibernate.dialect.Oracle10gDialect {   
  6.   
  7.     public OracleCustomDialect() {   
  8.         super();   
  9.         registerHibernateType(Types.FLOAT, Hibernate.FLOAT.getName());   
  10.     }   
  11. }  
 
這是自己解決的折中辦法,最好還是遵循標(biāo)準(zhǔn)類型
在hibernate中,Oracle9Dialect 方言定義有以下代碼:
Java代碼
  1. public class Oracle9Dialect extends Dialect {   
  2.   
  3.     private static final Logger log = LoggerFactory.getLogger( Oracle9Dialect.class );   
  4.   
  5.     public Oracle9Dialect() {   
  6.         super();   
  7.         log.warn( "The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead" );   
  8.         registerColumnType( Types.BIT, "number(1,0)" );   
  9.         registerColumnType( Types.BIGINT, "number(19,0)" );   
  10.         registerColumnType( Types.SMALLINT, "number(5,0)" );   
  11.         registerColumnType( Types.TINYINT, "number(3,0)" );   
  12.         registerColumnType( Types.INTEGER, "number(10,0)" );   
  13.         registerColumnType( Types.CHAR, "char(1 char)" );   
  14.         registerColumnType( Types.VARCHAR, 4000"varchar2($l char)" );   
  15.         registerColumnType( Types.VARCHAR, "long" );   
  16.         registerColumnType( Types.FLOAT, "float" );   
  17.         registerColumnType( Types.DOUBLE, "double precision" );   
  18.         registerColumnType( Types.DATE, "date" );   
  19.         registerColumnType( Types.TIME, "date" );   
  20.         registerColumnType( Types.TIMESTAMP, "timestamp" );   
  21.         registerColumnType( Types.VARBINARY, 2000"raw($l)" );   
  22.         registerColumnType( Types.VARBINARY, "long raw" );   
  23.         registerColumnType( Types.NUMERIC, "number($p,$s)" );   
  24.         registerColumnType( Types.DECIMAL, "number($p,$s)" );   
  25.         registerColumnType( Types.BLOB, "blob" );   
  26.         registerColumnType( Types.CLOB, "clob" );   
  27.   
  28. ……   
  29. ……  
 
而hibernate 3.3.2中 帶的Oracle10gDialect方言代碼如下,是繼承了 Oracle9Dialect
Java代碼
  1. public class Oracle10gDialect extends Oracle9iDialect {   
  2.   
  3.     public Oracle10gDialect() {   
  4.         super();   
  5.     }   
  6.   
  7.     public JoinFragment createOuterJoinFragment() {   
  8.         return new ANSIJoinFragment();   
  9.     }   
  10. }  
 
同時(shí),在public abstract class Dialect 類中,定義了以下registerHibernateType, 所有的數(shù)據(jù)庫類型的Dialect都是 extends Dialect
Java代碼
  1. protected Dialect() {   
  2.     log.info( "Using dialect: " + this );   
  3.     sqlFunctions.putAll( STANDARD_AGGREGATE_FUNCTIONS );   
  4.   
  5.     // standard sql92 functions (can be overridden by subclasses)   
  6.     registerFunction( "substring"new SQLFunctionTemplate( Hibernate.STRING, "substring(?1, ?2, ?3)" ) );   
  7.     registerFunction( "locate"new SQLFunctionTemplate( Hibernate.INTEGER, "locate(?1, ?2, ?3)" ) );   
  8.     registerFunction( "trim"new SQLFunctionTemplate( Hibernate.STRING, "trim(?1 ?2 ?3 ?4)" ) );   
  9.     registerFunction( "length"new StandardSQLFunction( "length", Hibernate.INTEGER ) );   
  10.     registerFunction( "bit_length"new StandardSQLFunction( "bit_length", Hibernate.INTEGER ) );   
  11.     registerFunction( "coalesce"new StandardSQLFunction( "coalesce" ) );   
  12.     registerFunction( "nullif"new StandardSQLFunction( "nullif" ) );   
  13.     registerFunction( "abs"new StandardSQLFunction( "abs" ) );   
  14.     registerFunction( "mod"new StandardSQLFunction( "mod", Hibernate.INTEGER) );   
  15.     registerFunction( "sqrt"new StandardSQLFunction( "sqrt", Hibernate.DOUBLE) );   
  16.     registerFunction( "upper"new StandardSQLFunction("upper") );   
  17.     registerFunction( "lower"new StandardSQLFunction("lower") );   
  18.     registerFunction( "cast"new CastFunction() );   
  19.     registerFunction( "extract"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(?1 ?2 ?3)") );   
  20.   
  21.     //map second/minute/hour/day/month/year to ANSI extract(), override on subclasses   
  22.     registerFunction( "second"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(second from ?1)") );   
  23.     registerFunction( "minute"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(minute from ?1)") );   
  24.     registerFunction( "hour"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(hour from ?1)") );   
  25.     registerFunction( "day"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(day from ?1)") );   
  26.     registerFunction( "month"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(month from ?1)") );   
  27.     registerFunction( "year"new SQLFunctionTemplate(Hibernate.INTEGER, "extract(year from ?1)") );   
  28.   
  29.     registerFunction( "str"new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as char)") );   
  30.   
  31.        // register hibernate types for default use in scalar sqlquery type auto detection   
  32.     registerHibernateType( Types.BIGINT, Hibernate.BIG_INTEGER.getName() );   
  33.     registerHibernateType( Types.BINARY, Hibernate.BINARY.getName() );   
  34.     registerHibernateType( Types.BIT, Hibernate.BOOLEAN.getName() );   
  35.     registerHibernateType( Types.CHAR, Hibernate.CHARACTER.getName() );   
  36.     registerHibernateType( Types.DATE, Hibernate.DATE.getName() );   
  37.     registerHibernateType( Types.DOUBLE, Hibernate.DOUBLE.getName() );   
  38.     registerHibernateType( Types.FLOAT, Hibernate.FLOAT.getName() );   
  39.     registerHibernateType( Types.INTEGER, Hibernate.INTEGER.getName() );   
  40.     registerHibernateType( Types.SMALLINT, Hibernate.SHORT.getName() );   
  41.     registerHibernateType( Types.TINYINT, Hibernate.BYTE.getName() );   
  42.     registerHibernateType( Types.TIME, Hibernate.TIME.getName() );   
  43.     registerHibernateType( Types.TIMESTAMP, Hibernate.TIMESTAMP.getName() );   
  44.     registerHibernateType( Types.VARCHAR, Hibernate.STRING.getName() );   
  45.     registerHibernateType( Types.VARBINARY, Hibernate.BINARY.getName() );   
  46.     registerHibernateType( Types.NUMERIC, Hibernate.BIG_DECIMAL.getName() );   
  47.     registerHibernateType( Types.DECIMAL, Hibernate.BIG_DECIMAL.getName() );   
  48.     registerHibernateType( Types.BLOB, Hibernate.BLOB.getName() );   
  49.     registerHibernateType( Types.CLOB, Hibernate.CLOB.getName() );   
  50.     registerHibernateType( Types.REAL, Hibernate.FLOAT.getName() );  
 
如果有特殊類型確實(shí)需要,只能自定義。
所以oracle的類型定義最好遵循以上的 java.sql.Types,
在hibernate.cfg.xml中使用 org.hibernate.dialect.OracleCustomDialect
以防出現(xiàn)No Dialect mapping for JDBC type錯誤
其他的 數(shù)據(jù)庫類型,SQL SERVER  DB2 MYSQL 等等,都可以查看 hibernate的相關(guān)源碼找出個(gè)所以然來

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多