简体   繁体   English

执行本机插入查询后获取实体 ID + Spring JPA

[英]Get Entity ID after executing Native insert query + Spring JPA

I am using a Native query in my JPA Repository to run INSERT query - because, I couldn't run a few queries through JPA.我在我的 JPA 存储库中使用本机查询来运行 INSERT 查询 - 因为,我无法通过 JPA 运行一些查询。

String INSERT_USER_IN_TO_DATABASE = "INSERT INTO USER_MASTER " +
            "(" +
            "MOBILE_X,USER_TYPE,COUNTRYCODE,USER_N,USER_LAST_N,GENDER,DOB) " +
            "VALUES ( " +
            "EncryptByKey(Key_GUID(convert(varchar,?1)), ?2)," +
            "1,+91,?3,?4,?5,?6" +
            ")";

    @Query(value = INSERT_USER_IN_TO_DATABASE, nativeQuery = true)
    @Modifying
    UserMasterEntity saveNewUser(String encryptionKey,
                                 String phoneNumber,
                                 String name,
                                 String lastName,
                                 String gender,
                                 String dob);

My intention is to execute the INSERT statement and get me the userMaster entity.我的意图是执行INSERT语句并获取 userMaster 实体。 But I get the below exception但我得到以下异常

Caused by: java.lang.IllegalArgumentException: Modifying queries can only use void or int/Integer as return type! Offending method: public abstract com.officeride.fileprocess.job.bulkuser.UserMasterEntity com.officeride.fileprocess.job.bulkuser.UserMasterRepository.saveNewUser(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)

I used @ColumnTransformer stuff too in my entity and nothing is working out for me.我在我的实体中也使用@ColumnTransformer的东西,但对我来说没有任何效果。

How to tackle this?如何解决这个问题?

You cannot make the INSERT INTO statement return anything.您不能使INSERT INTO语句返回任何内容。 Depending on your database's support, on that end you could execute multiple statements, eg, INSERT INTO...; SELECT...根据您的数据库的支持,您可以执行多个语句,例如, INSERT INTO...; SELECT... INSERT INTO...; SELECT... , but Spring Data JDBC does not support this. INSERT INTO...; SELECT... ,但 Spring 数据 JDBC 不支持此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM