JPA仓库读取、删除和保存具有旧ID的对象

huangapple go评论63阅读模式
英文:

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,有两个解决方案(顺便提一下,并非所有可能的解决方案):

  1. id字段中移除@GeneratedValue注解,但是如果这样做,您必须为要保存的每个实体提供一个id

  2. 直接使用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):

  1. Remove the @GeneratedValue annotation from your id field, but if you do this you have to supply an id for each entity you want to save.

  2. 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).

huangapple
  • 本文由 发表于 2020年4月8日 19:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定