• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Data is getting updated instead of getting Inserted

 
Ranch Hand
Posts: 51
Eclipse IDE Spring Java
  • 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.
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser 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 Spring Java
  • 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.


 
Sheriff
Posts: 67399
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 Spring Java
  • 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 Spring Java
  • 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
Sheriff
Posts: 67399
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic