英文:
JPA repository read, delete and save Object with old id
问题
我正在尝试从存储库中读取 PriceObject
的 ID,然后将其删除,并将其另存为具有相同旧 ID 的新 PriceObject
,但它保存时具有不同的 ID。
这是我的方法:
public void getActualPricesAndSaveInDb() {
try {
String symbolString = symbol.getSymbol();
PriceObject oldPriceObject = pricesRepository.findByCompanySymbol(symbolString);
PriceObject newPriceObject = new PriceObject();
if (oldPriceObject != null) {
pricesRepository.deleteByCompanySymbol(symbolString);
newPriceObject.setId(oldPriceObject.getId());
}
newPriceObject.setCompanySymbol(symbolString);
pricesRepository.save(newPriceObject);
} catch (APICommandConstructionException | APIReplyParseException | APICommunicationException
| APIErrorResponse e) {
e.printStackTrace();
}
}
这是我的 PriceObject.class:
@Entity
@Data
public class PriceObject {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
Integer id;
@Column(nullable = false)
String companySymbol;
...
}
我迄今为止尝试过:
- 将注解
@GeneratedValue
的参数更改为(strategy = GenerationType.AUTO)
, - 将注解
@GeneratedValue
的参数更改为(strategy = GenerationType.SEQUENCE)
。
英文:
I am trying to read Pricebject
ID from repository, delete it and save it as new PriceObject
again with old ID, but it saves with different ID.
This is my method:
public void getActualPricesAndSaveInDb() {
try {
String symbolString = symbol.getSymbol();
PriceObject oldPriceObject = pricesRepository.findByCompanySymbol(symbolString);
PriceObject newPriceObject = new PriceObject();
if (oldPriceObject != null) {
pricesRepository.deleteByCompanySymbol(symbolString);
newPriceObject.setId(oldPriceObject.getId());
}
newPriceObject.setCompanySymbol(symbolString);
pricesRepository.save(newPriceObject);
} catch (APICommandConstructionException | APIReplyParseException | APICommunicationException
| APIErrorResponse e) {
e.printStackTrace();
}
}
This is my PriceObject.class:
@Entity
@Data
public class PriceObject {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
Integer id;
@Column(nullable = false)
String companySymbol;
...
}
I've tried so far:
> - change annotation @GeneratedValue
parameter for (strategy = GenerationType.AUTO)
,
>
> - change annotation @GeneratedValue
parameter for (strategy = GenerationType.SEQUENCE)
.
答案1
得分: 3
直接使用实体设置id
,如果您指定了生成id
的策略,将不起作用。
要设置所需实体的id
,有两个解决方案(顺便提一下,并非所有可能的解决方案):
-
从
id
字段中移除@GeneratedValue
注解,但是如果这样做,您必须为要保存的每个实体提供一个id
。 -
直接使用
EntityManager
创建本地查询。entityManager.createNativeQuery("insert into table(id, ...) values (id,...)");
英文:
Setting the id
directly using the entity won't work if you specify a strategy for generating the id
.
To set the id
of the entity you want you have two solution (not all possible solution by the way):
-
Remove the
@GeneratedValue
annotation from yourid
field, but if you do this you have to supply anid
for each entity you want to save. -
Create a native query directly using the
EntityManager
.entityManager.createNativeQuery("insert into table(id, ...) values (id,...)");
答案2
得分: 1
我不确定我是否正确理解您的问题,但可能是由于生成的值造成的。也许如果您不需要它用于任何其他方法,您可以尝试删除那个注释,或者用这个注释进行更改:@Column(unique = true)
。
英文:
I'm not sure I'm understanding your problem correctly but it might be due to the generated value. Maybe you could try deleting that annotation if you don't need it for any other method, or changing it with this one: @Column(unique = true)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论