Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EntityManager.persist(obj) is not actually storing the object in Database

 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am new to EJB.
I trying to store an Entity bean into Database using EntityManager.persist() method.
However I can see that object is not going into the database and no error or exception is shown on the console.
In contrast to this the same entity manager is deleting and updating the beans and working fine.
What may be the reason.

Below is part of my persistence.xml





The Entity bean as below






Please suggest.

Best Regards,
Rakesh
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class to be a JPA Entitty it needs to have the @Id in it.
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If this is the case. Neither updates nor delete should be performed on the same.
However the same is being performed.
So now what may be the reason behing entity not getting persisted into the database.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rakesh kadulkar wrote:Hi,

If this is the case. Neither updates nor delete should be performed on the same.
However the same is being performed.
So now what may be the reason behing entity not getting persisted into the database.

Or maybe to merge it do not need a @Id.

I readed 3 books that says that it needs the @Id to a class to be considered an entity, maybe your provider do not need this for the update.
 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If persist is not working, I am not sure how the entities are present in the database to be updated or deleted. Are they being manually created by using some db connecting utility?
If so, I can think of one particular issue. In case of persist, the db has to know what value to use as Id/primary key. But if you manually insert entities in db with some random id, update or delte using that Id will not be an issue with the db since the id is already there.
May be you can use something like auto generation strategy for primary key and then try to persist the entities and see if that works.
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Thanks for the replies.
I had been through the links that are provided. Those will be useful to me in the future.
But my problem is not yet resolved. Here below i am sharing the entire class and the id class





Your help in this regards is appreciated.
Best Regards,
Rakesh
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lokesh chenta wrote:If persist is not working, I am not sure how the entities are present in the database to be updated or deleted. Are they being manually created by using some db connecting utility?
If so, I can think of one particular issue. In case of persist, the db has to know what value to use as Id/primary key. But if you manually insert entities in db with some random id, update or delte using that Id will not be an issue with the db since the id is already there.
May be you can use something like auto generation strategy for primary key and then try to persist the entities and see if that works.


Yes you are write the records are already present in the database. They are inserted using the sql developer tool.
However the records are updated/deleted using EJB. And they are getting updated/deleted successfully.
However when i am inserting using EJB it is not getting inserted and further it does not shows any error in the logs.

I cannot use sequence/auto generation as I am suing composite primary key.
I have posted the structure beloe.
Please refer to it and suggest me what may be going wrong here.

Best Regards,
Rakesh
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi any update on the same.
If I try to create a record manually and try to insert it into the database the record gets inserted successfully but I am getting an object from the database,
deleting it into the database and then updating the state of the object and then trying to persists the same object back into the database however i find that it does not gets inserted into the database. Please let me know what may be the error.

Regards,
Rakesh
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...deleting it into the database and then updating the state of the object and then trying to persists the same object back into the database however i find that it does not gets inserted into the database.

Can you show the code for this?
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code is scattered all around and cannot be placed here.
There are multuple wrappers.
However if I use the same code to insert a fresh self created object into the database then it works.

Regards,
Rakesh
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is scattered all around and cannot be placed here.
There are multuple wrappers.

Then it might be something to do with session/transaction association of the object you are trying to save. Since you don't have anything in the logs (which seems strange), however it might worth adding some debug information around the code where you save this object and see?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic