wei1111
wei1111

Reputation: 3

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.

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. How do I do it? Thank you for your answer.

Upvotes: 0

Views: 3633

Answers (3)

pirho
pirho

Reputation: 12205

While the answer from Alain Cruz might work fine I would not do it like you have planned.

I would just add a standard id field and set the productId unique, like:

@Id
@GeneratedValue
private Long id;

@Column(unique=true)
private String productId;

Then in the ProductInfo repository just add something like:

Optional<ProductInfo> findByProductId(String productId);

Upvotes: 0

Alien
Alien

Reputation: 15878

Hibernate needs an id so add generation type to id and make productId unique like below and let the hibernate handle everything.

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

@Column(unique=true)
private String productId;

Upvotes: 0

Alain Cruz
Alain Cruz

Reputation: 5097

From reading the Hibernate documentation, the save operation only persist entities with auto generated ids. So, if you intend to set the id yourself, then what you need, is to change your insert method for 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.

Upvotes: 1

Related Questions