JPA: the identifier column is set both automatically and manually

I am using MySQL and have the following object:

class MyEntity {
    @Id
    @GeneratedValue(strategy=IDENTITY)
    @Column(name="id")
    private Integer id;
}

However, I would still like to set the identifier manually!

The problem is that INSERT will never insert an id column. For example, when doing this:

MyEntity e = new MyEntity();
e.setId(15);
em.persist(e);
em.flush();
em.refresh(e);

The following requests will be made:

INSERT INTO myEntities (col1, col2, col3) VALUES (?, ?, ?)
SELECT id, col1, col2, col3 FROM myEntities WHERE (id = 15)

And thus, an identifier will be automatically generated, and there refresh()will be a result EntityNotFoundException.

If I don’t install at all @GeneratedValue, then this case will work fine, but when the value is auto- LAST_INSERT_ID()generated , it will not be called.

Is it possible for identity columns to be set both manually and automatically, as in MySQL?

+3
2

EclipseLink, , TABLE SEQUENCE, IDENTITY.

0

JPA ( JDO); , , . DataNucleus , @Extension (key = "strategy-when-notnull", value = "true" ) , "id", , . , - ?

- (DataNucleus)

+2

Source: https://habr.com/ru/post/1708718/


All Articles