Spring data jpa save can not get id

前端 未结 7 1285
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 01:09

My entity id is generated, and it was working fine when I use DAO instead of Spring data JPA.

@Id
@Column(name = TABLE_COLUM_NAME_ID)
@GeneratedValue
private         


        
相关标签:
7条回答
  • 2020-12-10 01:43
    1. You should use the repository.saveAndFlush(); method:

    MyObject savedObject= repository.saveAndFlush(newObject);

    1. In the Entity object description, you should add the following attribute(@GeneratedValue(strategy = GenerationType.AUTO)) to the related field(id):
    @Id   
    @Column(name = "id")   
    @GeneratedValue(strategy = GenerationType.AUTO)   
    public long getId() {  
        return id;  
    }
    
    0 讨论(0)
提交回复
热议问题