• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data is getting updated instead of getting Inserted

 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Here is the detailed description of the problem.
I am trying to fetch a record lets say (primary key id=10).
Then I am updating it, say changing some fields.
Now I am saving it (entityManager.persist(Object)).
So in my console all I am getting is a update statement but I want it to be inserted as a new row. What I need to change?


I guess its because of the id. Since the object has id=10 ,hibernate is running an update statement. I tried to make the id as 0 before persisting but got exception.
Please help me. I am not pasting any code as I feel it is not needed in this case.
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What key generation is the object or entity using? Auto-increment, manually assign etc? If the entity has NO id or id==null then a create should occur.
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnks for the quick reply
i m using this annotation
@id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;


the id is int so I can not make it as null and I already tried 0 but it didn't work. I searched in internet and I understood that if we try to modify a bean and then again save it, its gets updated in hibernate by default.
But in this case, i want to modify and then insert.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Kr Agrawal wrote:But in this case, i want to modify and then insert.

Why? That's an odd thing to do.

If you want to create a new instance, you actually need to create a new, disconnected instance and then save it.
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I am still not clear about it.
Let me paste my DAO class


Please let me know exactly what needs to be done.
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally solved it. The bean needs to be instantiated again.as
bean=retrieveData(entity);
AppBean newBean=new AppBean();
newBeean.set(bean.get...);

now it will be inserted as new record. :-)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as I said, you ned to create a new instance that's not persisted. The instance you fetched is persisted, not disconnected. This is an important concept to understand in ORMs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic