Today's demand is met with a problem, hibernate execution Save () method times wrong: Java.lang.ClassCastException:java.lang.String cannot is cast to Java.math.BigDecimal. Search the Internet, almost all of the answers are that the entity, mapping file and table structure between the type does not correspond. Examined several times did not find the problem, had to think of other ways to achieve, the result of writing other methods of code when the problem is found.
I go to the data in a view through Hibernate, the view is a single table of a database connected via Dblink, the database primary key that the service accesses directly is the varchar () type, the remote database primary key accessed by DBLIKN is the number type, When I write the mapping file without checking the primary key generation policy is written as UUID, so when inserting the string value to insert the number type of the primary key will be an error. So the primary key generation strategy is changed to identity or sequence can be solved.
During the modification, hibernate configuration primary key generation policy was found to directly invoke the sequence of the remote database.
<IDname= "XXXX"type= "Java.math.BigDecimal">
<columnname= "XXXX"Precision= "XX" Scale= "0">
<Comment>Xxxx</Comment>
</column>
<Generatorclass= "sequence" >
<paramname= "sequence">dblikn.Xxxx</param>
</Generator>
</ID>
By the way, the other workaround mentioned earlier is to execute the SQL statement directly through hibernate.
String sql = "INSERT INTO XXXX" (Select (select Sys_guid () from dual), XXXX from [email protected]) "; Super. Getcurrentsession (). createsqlquery (SQL). Executeupdate ();
Not finished, basically this is a few lines of code. Using a different primary key generation strategy is to write the corresponding primary key in the code. For example, sequence to invoke an existing sequence. In addition, Oracle does not support identity or the first time it knows.
Hibernate save Times Java.lang.ClassCastException