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
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.
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;
I've received this error before when accidentally attempting to persist an entity with one of it's fields having a null value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With