Spring Data JDBC: DataRetrievalFailureException : Unable to cast [oracle.sql.ROWID] to [java.lang.Number]

六眼飞鱼酱① 提交于 2019-12-02 08:56:41

问题


I am new to Spring Data JDBC, and I am struggling to create a simple Dto and get it persisted on the DB.

I am using Spring-Boot 2.1.1.RELEASE and and Oracle 12 Database.

UserDto

@Table(value="USERS_T")
public class UserDto extends PersistableDto {
    @Id
    @Column(value="USR_USERNAME")
    private String userName;

    @Column(value="USR_FIRSTNAME")
    private String firstName;

    @Column(value="USR_LASTNAME")
    private String lastName; 
.....
}

UserDao

@Repository
public interface UserDao extends CrudRepository<UserDto, String> {

    @Query("SELECT * FROM USERS_T u WHERE u.USR_USERNAME = :userName")
    UserDto findByUserName(@Param("userName") String userName);
}

and I am simply trying to persist it on the DB like this

public String createUser() {
    UserDto userDto = new UserDto().setUserName("mapss@sapot.wrong.email.pt").setPassword("superpass").setUserType("Guest").setActive(true);
    logger.info(String.format("Creating user: " + userDto));

    userDto.setNew(true);
    UserDto persistedUser = userDao.save(userDto);

    logger.info(String.format("Persisted user: " + persistedUser));
    return "Ending of create user operation";
}

I am getting this exception.

org.springframework.dao.DataRetrievalFailureException: The generated key is not of a supported numeric type. Unable to cast [oracle.sql.ROWID] to [java.lang.Number]
        at org.springframework.jdbc.support.GeneratedKeyHolder.getKey(GeneratedKeyHolder.java:79) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.data.jdbc.core.DefaultDataAccessStrategy.getIdFromHolder(DefaultDataAccessStrategy.java:323) ~[spring-data-jdbc-1.0.3.RELEASE.jar:1.0.3.RELEASE]

I believe that this somehow is related with the fact that the @Id is a String.

Can someone help me understand what am I doing wrong? Why this behavior. On the spec I do not see and restriction to the type of the Id. Is this and Oracle integration issue? How can i fix this?

thank you all for your help.


回答1:


Unfortunately, Oracle is not yet fully supported. There is an issue open for creating integration tests for Oracle and the accompanying PR is already fixing some issues, but certainly not all.

The main problem here is that Oracle does some interesting stuff regarding generated key generation. I see the following options

a) Don't use key generation on the database side. DATAJDBC-282 makes this more comfortable. But it is so far only in the SNAPSHOT release.

b) Don't use Oracle. We currently test with MySql, Postgres, H2, HSQLDB and MariaDb

c) Take a look at the PR mentioned above to see if you can patch it enough to work.

I'm aware that these options aren't very satisfying. The challenge is that it is really hard for an Open Source project to do integration tests with Oracle, since even downloading a legal Oracle JDBC driver from a public CI build is a nightmare, let alone a database.

A coworker sent me this image when we were discussing the situation:

But we don't give up, proper support will be added.




回答2:


I suffered the same problem, while the PR is being included in the next Spring Data JDBC release we can use the following workaround with Spring AOP, It's not "perfect" but enough for us until the underlying problem is solved:

@Around("execution(public * my-app-pacakage.repository.*.save(..))")
public Object aspectController(ProceedingJoinPoint jp) throws Throwable {
    try {
        return jp.proceed();
    } catch (DbActionExecutionException e) {
        if (e.getCause() instanceof DataRetrievalFailureException) {
            return jp.getArgs()[0];
        }
        return e;
    } catch(Throwable e) {
        throw e;        
    }       
}



回答3:


I think you need to declare the USR_ID field and corresponding sequence in your entity

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XXXX")
@SequenceGenerator(sequenceName = "YYYY", allocationSize = 1, name = "XXXX")
Long USR_ID;


来源:https://stackoverflow.com/questions/54239345/spring-data-jdbc-dataretrievalfailureexception-unable-to-cast-oracle-sql-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!