1

I have my entity called Post which has Id as a primary key.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
public Integer getId() {
    return Id;
}

public void setId(Integer id) {
    Id = id;
}

In above code you may see GenerationType.IDENTITY which I would assume is responsible for generating a new ID in case of creation of a new Post.

However, when I make a HTTP POST with this JSON:

{"id":0,"title":"test","viewCount":0,"body":"test","tags":"","answerCount":0,"commentCount":0,"postTypeId":1,"favoriteCount":0,"creationDate":"Nov 25, 2015 11:43:22 AM","acceptedAnswerId":0,"lastEditorUserId":0,"score":0}

I am getting this error:

 SEVERE: Servlet.service() for servlet [rest] in context with path [/StackExchange] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause
java.sql.SQLException: Field 'Id' doesn't have a default value

I haven't set AI though, maybe that is the cause. But I am unable to alter the table. The data in the table are from SQL dumps and I can't alter it to set Id as AutoIncrement

Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103
  • Which database are you using? – Predrag Maric Nov 25 '15 at 11:09
  • MySQL should support `GenerationType.IDENTITY`. Are you sure the annotations are at the right place? By default, fields should be annotated, not getters. – Predrag Maric Nov 25 '15 at 11:16
  • No actually not and I was thinking that that might be the cause, I was just hoping Hibernate has it's own way. – Ondrej Tokar Nov 25 '15 at 11:17
  • 2
    You are already submitting an id of `0` which isn't `null` and hence no generation will be done. Don't submit the id. – M. Deinum Nov 25 '15 at 11:17
  • @M.Deinum that is not true, for integers it is 0 and therefore it would work. – Ondrej Tokar Nov 25 '15 at 11:17
  • @PetterFriberg You may choose, either having annotations on fields or on getters/setters, but you can't mix. – Ondrej Tokar Nov 25 '15 at 11:18
  • No that is for `int` not for `Integer`... Those are treated differently... – M. Deinum Nov 25 '15 at 11:19
  • Yes, I meant `int` you're right. But even if I don't post `id` the response is the same ;/ – Ondrej Tokar Nov 25 '15 at 11:19
  • You would need to switch the column to auto increment to make it work AFAIK. See http://stackoverflow.com/questions/970597/change-auto-increment-starting-number for the procedure how to fix the auto increment. – M. Deinum Nov 25 '15 at 11:33
  • @M.Deinum, thank you, I tried that but now I am getting this: `Duplicate entry '0' for key 'PRIMARY'` Even though the `AI` is set to begin from higher number than all my IDs are. – Ondrej Tokar Nov 25 '15 at 11:58
  • You still shouldn't provide an id with the post, it should be null, also as mentioned in an earlier comment (not by me) make sure that you aren't mixing field and getter/setter annotations as that won't work, use either fields or getter/setters JPA won't support mixing them. – M. Deinum Nov 25 '15 at 12:00
  • I am not posting Id anymore. I get the same error even if I do a regular SQL statement without Id. – Ondrej Tokar Nov 25 '15 at 12:01
  • @M.Deinum The reason I had the other error was my Trigger... thank you. Feel free to post it as an answer so I can accept it. – Ondrej Tokar Nov 25 '15 at 12:02

0 Answers0