0

My Entity contains Long id attribute and i get JSON request from user with id value,

When i tried to save it first time, it saving, and when i tried to use same id in JPA save method, it is saving also. I use the below,

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id", unique = true)
    private Long id;

and i tried with the below,

@Table(name = "Person",
        uniqueConstraints={
                @UniqueConstraint(columnNames = {"id"})})

also not working. My request JSON as below, and if i use the same JSON for next request also saving.

{
  "id":1,
  "name":"John"
}
prostý člověk
  • 909
  • 11
  • 29
  • your unique constraint is not working? – Ali Tahir Sep 16 '20 at 13:49
  • In my case the id from user JSON, yes not working. How to make them unique. Tried the above approach but no luck. – prostý člověk Sep 16 '20 at 13:50
  • try this: https://stackoverflow.com/questions/3126769/uniqueconstraint-annotation-in-java – Ali Tahir Sep 16 '20 at 13:51
  • 1
    when you pass an entity with id to the save method, instead of inserting a new database record makes an update so it does not trigger the DB restriction. The operation is correct. You can try to set name as unique key and insert {"id": 1, "name": "John"} and {"id": 2, "name": "John"} – JLazar0 Sep 16 '20 at 13:55
  • Unfortunately, i use some API, which uses inbuilt schema but it's grey area for me. That's one of the issue for me – prostý člověk Sep 16 '20 at 13:56
  • I tried with different id and same name, it's saving also. @AliTahir tried with ```@Table(name = "Person", uniqueConstraints={@UniqueConstraint(columnNames ={"id"})})``` also no luck. – prostý člověk Sep 16 '20 at 14:11
  • 1
    tried this? : uniqueConstraints={@UniqueConstraint(columnNames = "column1")} – Ali Tahir Sep 16 '20 at 14:20
  • I have tried this: @Table(name = "Person", uniqueConstraints = @UniqueConstraint(columnNames = "id")) ...... and its working on my machine. You can also try removing @GeneratedValue and see if it helps. – Ali Tahir Sep 16 '20 at 14:41
  • sure, Thanks Tahir i will try now, and keep you posted. – prostý člověk Sep 16 '20 at 14:43
  • Tahir, i used tried, ```@Table(name = "Person", uniqueConstraints = @UniqueConstraint(columnNames = "id")) public class Person { @Id @Column(name = "id") private Long id;``` no luck. – prostý člověk Sep 16 '20 at 14:53
  • well, it looks like you will have to get a list of ids from DB and check if it already exists in your service class. atm in one of my projects I'm sending requests while the user is typing a unique value for better user experience. – Ali Tahir Sep 16 '20 at 15:06
  • yeah, that's my next approach. thanks Tahir. – prostý člověk Sep 16 '20 at 15:17

1 Answers1

1

The problem could be in code.

When you say strategy=GenerationType.IDENTITY and passing the exist ID, hibernate believes that this entity already in DB because it has ID.

IDENTITY means your DB has column "id" and it auto-incremented while insert is happened.

If you always receive entity with exist ID, just remove @GeneratedValue(strategy=GenerationType.IDENTITY) and I hope it would be okey.

Vielen Danke
  • 177
  • 1
  • 6