inconsistent datatypes: expected NUMBER got BINARY

2020-07-11 09:52发布

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条回答
\"骚年 ilove
2楼-- · 2020-07-11 10:24

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

private Long userid;
查看更多
狗以群分
3楼-- · 2020-07-11 10:31

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;
查看更多
Juvenile、少年°
4楼-- · 2020-07-11 10:32

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.

查看更多
做个烂人
5楼-- · 2020-07-11 10:37

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

查看更多
登录 后发表回答