I have and entity with two fields an id and a code as so…
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Basic
@Column(name = "code", nullable = false)
private String code;
What I would like is to do is, to generate the code based on the Auto-incremented id (Using Mysql as DB).
Is there a way without using a table to generate keys for the code column.
i.e
- id = 1 code is M-000-1
- id = 2 code is M-000-2 etc..
The only way I have managed to do this is using the script below. Which I am not so sure is the correct way.
getEntityManager().persist(myEntity);
getEntityManager().flush();
myEntity.setCode(myEntity.getCode()+myEntity.getId());
getEntityManager().merge(myEntity);
Thanks in advance!
Dimitri
Looks fine. You can perhaps move it to a
@PostPersisthandler.