Ket
Ket

Reputation: 551

java.sql.SQLException: Fail to convert to internal representation

I'm trying execute following query:

String query = "select entity, entity.id from Site entity";
List resultList = entityManager.createQuery(query).getResultList();

And take exception:

[...]
Caused by: java.sql.SQLException: Fail to convert to internal representation
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.jdbc.driver.CharCommonAccessor.getLong(CharCommonAccessor.java:239)
    at oracle.jdbc.driver.OracleResultSetImpl.getLong(OracleResultSetImpl.java:552)
    at oracle.jdbc.driver.OracleResultSet.getLong(OracleResultSet.java:1575)
    at org.jboss.resource.adapter.jdbc.WrappedResultSet.getLong(WrappedResultSet.java:724)
    at org.hibernate.type.LongType.get(LongType.java:28)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
    at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:103)
    at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:204)
    at org.hibernate.loader.hql.QueryLoader.getResultColumnOrRow(QueryLoader.java:338)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
    at org.hibernate.loader.Loader.doQuery(Loader.java:701)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2220)
    ... 102 more

What am I doing wrong? Thank you in advance.

Upvotes: 55

Views: 215950

Answers (5)

Ashish
Ashish

Reputation: 1021

Your data types are mismatched when you are retrieving the field values. Check your code and ensure that for each field that you are retrieving that the java object matches that type. For example, "retrieving a date into an int".

If you are doing a select * then it is possible a change in the fields of the table has happened causing this error to occur. Your SQL should only select the fields you specifically want in order to avoid this error.

Upvotes: 102

Florian Zidar
Florian Zidar

Reputation: 81

Your data types are mismatched when you are retrieving the field values.

Also check how you store your enums, default is ORDINAL (numeric value stored in database), but STRING (name of enum stored in database) is also an option. Make sure the Entity in your code and the Model in your database are exactly the same.

I had an enum mismatch. It was set to default (ORDINAL) but the database model was expecting a string VARCHAR2(100char). Solution: @Enumerated(EnumType.STRING)

Upvotes: 4

BlackKat
BlackKat

Reputation: 420

Check your Entity class. Use String instead of Long and float instead of double .

Upvotes: 15

abdulsamed kayaduman
abdulsamed kayaduman

Reputation: 602

I had the same problem and this is my solution. I had the following code:

se.GiftDescription = rs.getString(1);
se.GiftAmount = rs.getInt(2);

And I changed it to:

se.GiftDescription = rs.getString("DESCRIPTION");
se.GiftAmount = rs.getInt("AMOUNT");

And the problem was, after I restarted my PC, the column positions changed. That's why I got this error.

Upvotes: 1

Sudha Susarita
Sudha Susarita

Reputation: 9

Check with your bean class. Column data type and bean datatype must be same.

Upvotes: 1

Related Questions