简体   繁体   English

使用 EntityManager NativeQuery 生成自增数并将数据插入 Oracle DB

[英]Generate an autoincrement number and insert data to Oracle DB using EntityManager NativeQuery

I am developing an ETL application using Spring.我正在使用 Spring 开发 ETL 应用程序。 I have the data to be inserted in String type and want to load the data using entityManager.createNativeQuery(query).executeUpdate() I do not have any entity here as the ETL application can be used for any datasource.我有要以 String 类型插入的数据,并想使用 entityManager.createNativeQuery(query).executeUpdate() 加载数据我这里没有任何实体,因为 ETL 应用程序可用于任何数据源。

To demonstrate the problem:为了证明这个问题:

The source data can be any valid JSON like {name: "John", age: 31, city: "New York"}源数据可以是任何有效的 JSON 例如{name: "John", age: 31, city: "New York"}

The application itself generates the INSERT query from here like: INSERT INTO TABLE_NAME (Name, Age, City) values ('John',31,'New York')应用程序本身从此处生成 INSERT 查询,例如: INSERT INTO TABLE_NAME (Name, Age, City) values ('John',31,'New York')

I am using an @PersistenceContext EntityManager em to connect to Oracle 12c.我正在使用@PersistenceContext EntityManager em连接到 Oracle 12c。 Thus, running em.createNativeQuery(query).executeUpdate does the job for me.因此,运行 em.createNativeQuery(query).executeUpdate 为我完成了这项工作。

However, when I try to use an own index within the target table, I am not able o figure out the correct method of doing so.但是,当我尝试在目标表中使用自己的索引时,我无法找出正确的方法。

I tried using KeyHolder class, but it works with JdbcTemplate and not EntityManager我尝试使用 KeyHolder class,但它适用于 JdbcTemplate 而不是 EntityManager

KeyHolder keyHolder = new GeneratedKeyHolder();

I am unable to use我无法使用

em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(entity);

as I can't create an entity here.因为我无法在这里创建实体。

What would be the correct mehtod of creating an own unique key here?在这里创建自己的唯一密钥的正确方法是什么?

I recommend switching to JdbcTemplate since it doesn't look like you are using any of the features of JPA anyway.我建议切换到JdbcTemplate ,因为它看起来不像您正在使用 JPA 的任何功能。

If you insist on using the EntityManager , I think you'll have to select the sequence number first using SELECT my_seq.nextval FROM DUAL and then use that for the insert statement.如果您坚持使用EntityManager ,我认为您必须首先使用SELECT my_seq.nextval FROM DUAL然后将其用于插入语句。

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

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