Spring Data JDBC:DataRetrievalFailureException:无法将 [oracle.sql.ROWID] 转换为 [java.lang.Number] [英] Spring Data JDBC: DataRetrievalFailureException : Unable to cast [oracle.sql.ROWID] to [java.lang.Number]

查看:155
本文介绍了Spring Data JDBC:DataRetrievalFailureException:无法将 [oracle.sql.ROWID] 转换为 [java.lang.Number]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Data JDBC 的新手,我正在努力创建一个简单的 Dto 并将其保留在 DB 上.

我使用的是 Spring-Boot 2.1.1.RELEASE 和 Oracle 12 数据库.

UserDto

@Table(value="USERS_T")公共类 UserDto 扩展 PersistableDto {@Id@Column(value="USR_USERNAME")私人字符串用户名;@Column(value="USR_FIRSTNAME")私人字符串名字;@Column(value="USR_LASTNAME")私人字符串姓氏;.....}

UserDao

@Repository公共接口 UserDao 扩展了 CrudRepository{@Query("SELECT * FROM USERS_T u WHERE u.USR_USERNAME = :userName")UserDto findByUserName(@Param("userName") String userName);}

我只是想像这样将它保存在数据库上

public String createUser() {UserDto userDto = new UserDto().setUserName("mapss@sapot.wrong.email.pt").setPassword("superpass").setUserType("Guest").setActive(true);logger.info(String.format("创建用户:" + userDto));userDto.setNew(true);UserDto persistedUser = userDao.save(userDto);logger.info(String.format("持久化用户:" + persistedUser));return "创建用户操作结束";}

我收到此异常.

org.springframework.dao.DataRetrievalFailureException:生成的键不是受支持的数字类型.无法将 [oracle.sql.ROWID] 转换为 [java.lang.Number]在 org.springframework.jdbc.support.GeneratedKeyHolder.getKey(GeneratedKeyHolder.java:79) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]在 org.springframework.data.jdbc.core.DefaultDataAccessStrategy.getIdFromHolder(DefaultDataAccessStrategy.java:323) ~[spring-data-jdbc-1.0.3.RELEASE.jar:1.0.3.RELEASE]

我相信这在某种程度上与@Id 是字符串这一事实有关.

有人能帮我理解我做错了什么吗?为什么会有这种行为.在规范中,我没有看到对 Id 类型的限制.这是和 Oracle 集成的问题吗?我该如何解决这个问题?

感谢大家的帮助.

解决方案

不幸的是,Oracle 尚未得到完全支持.有

但我们不会放弃,会增加适当的支持.

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.

解决方案

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.

这篇关于Spring Data JDBC:DataRetrievalFailureException:无法将 [oracle.sql.ROWID] 转换为 [java.lang.Number]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆