Total Pageviews

Sunday 18 March 2012

How to persist entity with auto increment Id

when create new entity class from database, if the primary key (for example "Id") with auto increment get annotation @NotNull. At this time, if you use entity manager try to persist an entity, neither set the Id value nor automaticly generate the Id.

Foe Example:

Employer e1 = new Employer();       
e1.setName(name);
em.persist(e1);

The above will get error

In the Employer entity class

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull.-----------------------------------------------------Delete it!!
@Basic(optional = false)
@Column(name = "ID")
private Integer id;

You will  fix the problem.

1 comment: