5

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

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

you can accept your own answer to gain reputation points. You want the check mark by 'counter' (currently 0) at outside the top-left margin. Good luck.
8

This problem may occur when we declare an Object type property with @Column annotation.

@Column(name="job_category")
private MiscTypeSetup jobCategory;

We should declare with @JoinColumn annotation.

@ManyToOne
@JoinColumn(name="job_category")
private MiscTypeSetup jobCategory;

1 Comment

Thanks. Hibernate should give error during beans creation
1

I've received this error before when accidentally attempting to persist an entity with one of it's fields having a null value.

Comments

0

Use Long as the data type instead. It might help.

private Long userid;

1 Comment

Still not working, I now get null from user.getUserid(); meaning the generator class="increment" not working (probably because userid in Users.hbm.xml is of type "int", I have try to change it to other types then I get the inconsistent datatypes error again). I don't understand something, I didn't created the Users class it was generated automatically with hibernate, so how come I need to make changes in this class, it's not suppose to create Users class with the right types respected to database table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.