简体   繁体   English

Hibernate Spring Data JPA如何使用ID保存对象

[英]hibernate Spring Data JPA how to save an object with id

public class ProductInfo {

    @Id
    private String productId;

    /** 名字. */
    private String productName;

    /** 单价. */
    private BigDecimal productPrice;
    ...

    @Test
    public void saveTest() {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId("123456");
        productInfo.setProductName("皮蛋粥");
        productInfo.setProductPrice(new BigDecimal(3.2));
        productInfo.setProductStock(100);
        productInfo.setProductDescription("很好喝的粥");
        productInfo.setProductIcon("http://xxxxx.jpg");
        productInfo.setProductStatus(0);
        productInfo.setCategoryType(3);

//        ProductInfo result = repository.save(productInfo);
        ProductInfo result = repository.saveAndFlush(productInfo);
        Assert.assertNotNull(result);
    }

I want to save an object with ID, which is not recorded in the database. 我想保存一个ID为ID的对象,该对象未记录在数据库中。

Hibernate: 
    select
        productinf0_.product_id as product_1_3_0_,
        productinf0_.category_type as category2_3_0_,
        productinf0_.create_time as create_t3_3_0_,
        productinf0_.product_description as product_4_3_0_,
        productinf0_.product_icon as product_5_3_0_,
        productinf0_.product_name as product_6_3_0_,
        productinf0_.product_price as product_7_3_0_,
        productinf0_.product_status as product_8_3_0_,
        productinf0_.product_stock as product_9_3_0_,
        productinf0_.update_time as update_10_3_0_ 
    from
        product_info productinf0_ 
    where
        productinf0_.product_id=?
Hibernate: 
    update
        product_info 
    set
        create_time=?,
        product_price=?,
        update_time=? 
    where
        product_id=?

According to hibernate's rules, it is impossible to save data to the database by selecting the object with ID first in update. 根据休眠规则,不可能通过在更新时首先选择ID为ID的对象来将数据保存到数据库中。 How do I do it? 我该怎么做? Thank you for your answer. 谢谢您的回答。

From reading the Hibernate documentation, the save operation only persist entities with auto generated ids. 通过阅读Hibernate文档,保存操作仅保留具有自动生成ID的实体。 So, if you intend to set the id yourself, then what you need, is to change your insert method for persist . 因此,如果您打算自己设置ID,那么您需要的是将insert方法更改为persist

@Test
public void saveTest() {
    ProductInfo productInfo = new ProductInfo();
    productInfo.setProductId("123456");
    productInfo.setProductName("皮蛋粥");
    productInfo.setProductPrice(new BigDecimal(3.2));
    productInfo.setProductStock(100);
    productInfo.setProductDescription("很好喝的粥");
    productInfo.setProductIcon("http://xxxxx.jpg");
    productInfo.setProductStatus(0);
    productInfo.setCategoryType(3);

    // Insert new product.
    repository.persist(productInfo);
}

For more information, you can check out this blog . 有关更多信息,您可以查看此博客

While the answer from Alain Cruz might work fine I would not do it like you have planned. 虽然Alain Cruz的答案可能很好用,但我不会像您计划的那样做。

I would just add a standard id field and set the productId unique, like: 我只需要添加一个标准ID字段并设置productId唯一即可,例如:

@Id
@GeneratedValue
private Long id;

@Column(unique=true)
private String productId;

Then in the ProductInfo repository just add something like: 然后在ProductInfo存储库中添加以下内容:

Optional<ProductInfo> findByProductId(String productId);

Hibernate needs an id so add generation type to id and make productId unique like below and let the hibernate handle everything. Hibernate需要一个id因此将生成类型添加到ID中,并使productId唯一,如下所示,然后让Hibernate处理所有事情。

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@Column(unique=true)
private String productId;

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

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