Rest and JPA, force Jersey the use of auto-increment instead of JSON object value?
Question
Ok so I am using Jersey for my REST services, and I'm trying to persist a batch of objects received in JSON.
This is my entity:
@Entity
@Table(name="element", uniqueConstraints=@UniqueConstraint(columnNames={"key","value"}))
public classElement implements Serializable {
@Id
@NotNull
private long key;
@NotBlank
@Size(max=30)
private String value;
@NotBlank
@Size(max=12)
private String more;
@NotNull
private long userid;
And this is my "create" method:
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createRecord(List<Element> elements) throws Exception{
for(Element element : elements){
violations = validator.validate(element);
if (violations.size() > 0) {
jsonResponse.setSuccess(false);
jsonResponse.setMessage("Error");
jsonResponse.setValidationError(this.getValidationErrorList());
}
else {
em.persist(valeur);
}
jsonResponse.setSuccess(true);
jsonResponse.setMessage("Success");
}
return Response.ok().entity(new ObjectMapper().writeValueAsString(jsonResponse)).build();
}
This works nicely when I have a json containing the value, more, and userid fields. The key gets an auto-incremented value. But the problem is, in my context I can't validate everything the "user" sends. So it is possible to send the json with the above fields AND the key (primary key). So I get a SQLError (duplicate entry) when it is persisted.
So my question (finally): What exactly happens when I do not specify any key and it gets an "auto" value, and how can I reproduce this even when the key is specify? (I.E. ignore the json key field and have the same auto-incremented value I would get without the field)
Thanks a lot!
No correct solution