How to set autogenerated Id manually?

Viewed 12550

I have an entity called Task and it has taskId field. Problem is I need to create/update some specific tasks and JPA autogenerator doesn't allow me to set taskId manually.(It overrides my taskId)

Is there any way to set taskId manually?

@Id
@Column(name = "task_id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private String taskId;
5 Answers

Just implement org.springframework.data.domain.Persistable;

After too much finding and frustration i have used this and worked for my R2DBMS entity

@Table(name = "...")
public class .... implements Persistable<String> {

   ...

   public boolean isNew(){
     return id==null; //modify and set your logic
   }

}
Related