GoldenAxe GoldenAxe - 6 months ago 63
Java Question

inconsistent datatypes: expected NUMBER got BINARY

I'm new to Hibernate, I'm trying to do a "simple" user insertion to oracle database I have created.

I created all the necessary files with Netbeans Hibernate wizards:
hibernate.cfg.xml, hibernate,reveng.xml, Users.hbm.xml, Users.java

If I insert user with the oracle sql developer, I can get this user from java code. But if I'm trying to insert a user I get the error: inconsistent datatypes: expected NUMBER got BINARY.

Partial Users.hbm.xml:

<hibernate-mapping>
<class name="HibernateDB.Users" schema="SYSTEM" table="USERS">
<id name="userid" type="int">
<column name="USERID" precision="9" scale="0"/>
<generator class="increment"/>
</id>
...


Partial Users.java:

public class Users implements java.io.Serializable
{

private int userid;
private String username;
private String password;
private String firstName;
private String lastName;
private Serializable dateOfBirth;
private Serializable registrationDate;
...


Partial insertUser method (all parameters are strings):

session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Calendar dOfBirth = Calendar.getInstance();
dOfBirth.set(Integer.parseInt(year_of_birth), Integer.parseInt(month_of_birth), Integer.parseInt(day_of_birth));
Calendar regDate = Calendar.getInstance();
Timestamp dOfBirthTS = new Timestamp(dOfBirth.getTimeInMillis());
Timestamp regDateTS = new Timestamp(regDate.getTimeInMillis());
Users user = new Users();
user.setUsername(username);
user.setPassword(password);
user.setFirstName(first_name);
user.setLastName(last_name);
user.setDateOfBirth(dOfBirthTS);
user.setRegistrationDate(regDateTS);
session.saveOrUpdate(user);
ans = user.getUserid();
tx.commit();


Users Table in the database:

USERID NUMBER(9,0) - the primary key
USERNAME VARCHAR(200)
PASSWORD VARCHAR(200)
FIRST_NAME VARCAHR(200)
LAST_NAME VARCHAR(200)
DATE_OF_BIRTH TIMESTAMP
REGISTRATION_DATE TIMESTAMP

Answer

I have found the real problem therefore I could solve it!

Real problem: Table have TIMESTAMP fields, hibernate generate them as Serializable, which produce the error as Serializable is not a TIMESTAMP.

Fix: I have add a mapping rule to hibernate.reveng.xml:

<hibernate-reverse-engineering>
  <schema-selection match-schema="SYSTEM"/>
    <type-mapping> 
        <sql-type jdbc-type="OTHER" hibernate-type="java.util.Calendar" /> 
    </type-mapping>
    ...

it also work's with Date type not just Calender (maybe more types I didn't try).

Conclusion: shouldn't relay on auto generating mechanism.

Comments