aspose file tools*
The moose likes Object Relational Mapping and the fly likes EntityManager.persist(obj) is not actually storing the object in Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EntityManager.persist(obj) is not actually storing the object in Database" Watch "EntityManager.persist(obj) is not actually storing the object in Database" New topic
Author

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

rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
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


Rakesh Kadulkar
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

A class to be a JPA Entitty it needs to have the @Id in it.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
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

Joined: Jul 14, 2010
Posts: 754

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

Joined: Oct 27, 2009
Posts: 100

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.


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Maybe this posts will help: JPA TableGenerator – Simple Primay Key, JPA SequenceGenerator
rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
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

Joined: Jul 24, 2008
Posts: 115
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

Joined: Jul 24, 2008
Posts: 115
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

Joined: Mar 24, 2008
Posts: 3855

...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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
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

Joined: Mar 24, 2008
Posts: 3855

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EntityManager.persist(obj) is not actually storing the object in Database