when querying Oracle using Hibernate's original eco-SQL, it always returns a character when querying the char type, starting to think that hibernate is mapping the data type to char (1), and after looking up some information on the web , knowing that the main reason for this problem is that when hibernate queries Oracle again, char is automatically mapped to a character (varchar subset) type. Oracle's char field is mapped to the character type in Hibernate and is a subset of varchar. The following workarounds are available:
1: Convert the entity you want to query into an entity bean
with HQL queries, each table has a mapping file configuration, and a reverse-engineered mapping file for multi-character char is automatically mapped to the string type in Java. There is no problem with field mapping, but it is not possible to use SQL;
2: You can use Addscalar (String arg,type type) in hibernate to customize the type of the returned field , such as:
This can be solved, but in this case, all the fields to be queried must be defined, when the field is more, it is relatively troublesome;
3: Specify data format directly in Oracle query statement , such as:
Select Id,name,cast (state as varchar (2)) from TB
This workaround is convenient, just to specify the return type of char in the query statement, it is recommended to use this method to query.
4. Modify Hibernate default char mapping typeInherits its dialect class implementation by overriding it. Custom Hibernate dialect. The database I'm using is Oracle:
First, a class is built, inheriting com.digitwater.fwk.hibernate.Oracle9iDialect, and the contents of this class are as follows:
public class Mysqldialect extends Mysql5dialect {public mysqldialect () {super (); Registerhibernatetype (Types.char, Hibernate.STRING.getName ()); }}
The second step is to modify Hibernate's configuration file Hibernate.cfg.xml:
Will:<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</prop>
Change to:<prop key= "Hibernate.dialect" >com.fy.mgr.support.MySQLDialect</prop>
This method change is minimal. The current use of normal, no other problems found temporarily.
5.
There are two ways to convert a SQL query out field to an object in Hibernate1. Using Resulttransformer, the code is as follows
Query.setresulttransformer (New Aliastobeanresulttransformer (Studentdto.class)). List ()
This approach requires that the database's field names and class properties have the same casing, but the field names returned by the Oracle query are all uppercase.
2. Using the Addentity method, the disadvantage of this method is that SQL must be written in the form of select * from users or select {a.*},{b.*} from a, b where ... The format
attached: Oracle cast () function usage:
Cast is a type conversion and can be used for a variety of Oracle data types. The user's data type is modified.
Round just modifies the data display format. Rounding the data. Similar functions also have ceil (the smallest integer that takes this data). Trunc (Take integer function.)
sql> CREATE TABLE T1 (a varchar ());
Table created.
sql> INSERT INTO T1 values (' 12.3456 ');
1 row created.
Sql> Select Round (a) from T1;
ROUND (A)
----------
12
Sql> Select Round (a,3) from T1;
ROUND (a,3)
----------
12.346
Sql> Select CAST (A as int) from T1;
CAST (Aasint)
------------
12
Sql> Select CAST (A as number (8,4)) from T1;
CAST (Aasnumber (8,4))
--------------------
12.3456
Hibernate createsqlquery Query Char type result is a single character resolution rollup