I'm using MySQL and have the following entity:
class MyEntity {
@Id
@GeneratedValue(strategy=IDENTITY)
@Column(name="id")
private Integer id;
}
However, I would still like to be able to set the id manually!
The problem is that INSERT will never insert the id column. E.g., when doing this:
MyEntity e = new MyEntity();
e.setId(15);
em.persist(e);
em.flush();
em.refresh(e);
The following queries will be made:
INSERT INTO myEntities (col1, col2, col3) VALUES (?, ?, ?)
SELECT id, col1, col2, col3 FROM myEntities WHERE (id = 15)
And thus the id will still be auto-generated and the refresh()
will
result in EntityNotFoundException
.
If I don't set @GeneratedValue
at all, then this case will work OK,
but when the value is autogenerated, LAST_INSERT_ID()
won't be called.
Is it at all possible to have id columns settable both manually and automatically, like in MySQL?