NonUniqueObjectException

I keep getting a strange error using a DB-VA generated ORM engine:

Exception:orm.PersistentException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

I understand what this error means but it is weird because after extensive testing I realized that it works sometimes and sometimes in doesn’t (everything else being constant). This occurs when I call the save function on a previously loaded object. If I make several calls within the same session to the same save() function, it eventually works.

Anyone haven’t the same problems? I’m starting to suspect a possible bug in DBVA’s orm classes…

Thank you for your message. Could you mind tell me you are working on the web application or standalone application? For web application, please make sure you have defined the ORM filter in the web.xml. For more details about the filter section of the XML, please select Generate Servlet sample when generating the persistence code. After that you should see a few lines of commented XML about the Filter at the bottom of the file. Please copy that code into your web.xml and reload the application. If you problem still persist, please let me know.

Best regards,
Rain

This could explain a lot! I’m using the beta version 3.0 of DBVA and when I select Generate servlet, nothing is generated. Can you post the ORM filter info? Thanks

[quote=Rain]Thank you for your message. Could you mind tell me you are working on the web application or standalone application? For web application, please make sure you have defined the ORM filter in the web.xml. For more details about the filter section of the XML, please select Generate Servlet sample when generating the persistence code. After that you should see a few lines of commented XML about the Filter at the bottom of the file. Please copy that code into your web.xml and reload the application. If you problem still persist, please let me know.

Best regards,
Rain
[/quote]

I am having a similar problem:

  • I have developed a webapp (with a bunch of beans with all three scopes from which all request for DB r/w are made) that call an external library (for all DB transactions) - This external library uses the appORM.jar generated with DBVA - Most transactions are performed by a session scoped beans and some by a request-scoped bean. How would the filter work in such an environment?

  • I installed the filter (which basically closes the session after each page request). Is this really needed? I’m assuming it’s not a good idea for when the request comes from a session scoped bean?

  • After installing the filter, I keep getting: “no session or session was closed” - does this mean that I have to create a session before every request if I decide to use the filter?

Do you have any documentation on how to use this in a web application environment?

Many thanks,

Jean

[quote=Rain]Thank you for your message. Could you mind tell me you are working on the web application or standalone application? For web application, please make sure you have defined the ORM filter in the web.xml. For more details about the filter section of the XML, please select Generate Servlet sample when generating the persistence code. After that you should see a few lines of commented XML about the Filter at the bottom of the file. Please copy that code into your web.xml and reload the application. If you problem still persist, please let me know.

Best regards,
Rain
[/quote]

The filter is look like this. The “XXFilter” is the name of the filter class (XX will be the project name). Please put it inside the web.xml and restart the server after adding it. The missing sample problem in EA version has been fixed and will be release very soon. If you have any further questions, please feel free to contact us again.

ORMFilter XXFilter ORMFilter /*

Best regards,
Rain

[quote=Anonymous]This could explain a lot! I’m using the beta version 3.0 of DBVA and when I select Generate servlet, nothing is generated. Can you post the ORM filter info? Thanks

[quote=Rain]Thank you for your message. Could you mind tell me you are working on the web application or standalone application? For web application, please make sure you have defined the ORM filter in the web.xml. For more details about the filter section of the XML, please select Generate Servlet sample when generating the persistence code. After that you should see a few lines of commented XML about the Filter at the bottom of the file. Please copy that code into your web.xml and reload the application. If you problem still persist, please let me know.

Best regards,
Rain
[/quote][/quote]

Dear Jean,

Thank you for your message. If you want the DB retrieved objects work across session, you have to lock it back to session in the next request. You can do it with the following code sample


PersistentManager.instance().getSession().lock(obj, LockMode.NONE);

where obj is your object.

You can get more information about how to use the DBVA and the generated persistence layer at http://uml.visual-paradigm.com/uml/Object_Relational_Mapping

If you have any further questions, please feel free to contact us again.

Best regards,
Rain

[quote=Anonymous]I am having a similar problem:

  • I have developed a webapp (with a bunch of beans with all three scopes from which all request for DB r/w are made) that call an external library (for all DB transactions) - This external library uses the appORM.jar generated with DBVA - Most transactions are performed by a session scoped beans and some by a request-scoped bean. How would the filter work in such an environment?

  • I installed the filter (which basically closes the session after each page request). Is this really needed? I’m assuming it’s not a good idea for when the request comes from a session scoped bean?

  • After installing the filter, I keep getting: “no session or session was closed” - does this mean that I have to create a session before every request if I decide to use the filter?

Do you have any documentation on how to use this in a web application environment?

Many thanks,

Jean

[quote=Rain]Thank you for your message. Could you mind tell me you are working on the web application or standalone application? For web application, please make sure you have defined the ORM filter in the web.xml. For more details about the filter section of the XML, please select Generate Servlet sample when generating the persistence code. After that you should see a few lines of commented XML about the Filter at the bottom of the file. Please copy that code into your web.xml and reload the application. If you problem still persist, please let me know.

Best regards,
Rain
[/quote][/quote]

Can you elaborate more on how the locking would work? It is not very clear from your reply and I cannot find it anywhere in the link you listed.

[quote=Rain]Dear Jean,

Thank you for your message. If you want the DB retrieved objects work across session, you have to lock it back to session in the next request. You can do it with the following code sample


PersistentManager.instance().getSession().lock(obj, LockMode.NONE);

where obj is your object.

You can get more information about how to use the DBVA and the generated persistence layer at http://uml.visual-paradigm.com/uml/Object_Relational_Mapping

If you have any further questions, please feel free to contact us again.

Best regards,
Rain

[quote=Anonymous]I am having a similar problem:

  • I have developed a webapp (with a bunch of beans with all three scopes from which all request for DB r/w are made) that call an external library (for all DB transactions) - This external library uses the appORM.jar generated with DBVA - Most transactions are performed by a session scoped beans and some by a request-scoped bean. How would the filter work in such an environment?

  • I installed the filter (which basically closes the session after each page request). Is this really needed? I’m assuming it’s not a good idea for when the request comes from a session scoped bean?

  • After installing the filter, I keep getting: “no session or session was closed” - does this mean that I have to create a session before every request if I decide to use the filter?

Do you have any documentation on how to use this in a web application environment?

Many thanks,

Jean

[quote=Rain]Thank you for your message. Could you mind tell me you are working on the web application or standalone application? For web application, please make sure you have defined the ORM filter in the web.xml. For more details about the filter section of the XML, please select Generate Servlet sample when generating the persistence code. After that you should see a few lines of commented XML about the Filter at the bottom of the file. Please copy that code into your web.xml and reload the application. If you problem still persist, please let me know.

Best regards,
Rain
[/quote][/quote][/quote]

The reason that need the ORM Filter is to avoid the multiple access and concurrency problems. Also it helps to keep the session small for enhancing performance. Since every request is a new session, so we provide the lock method for associate the object to the new ORM session.

If you have any further questions, please feel free to contact us again.

Best regards,
Rain

I keep getting: “no session or session was closed” - does this mean that I have to create a session before every request if I decide to use the filter? How is this done?

Thanks for replying. If you use the filter, the session will be closed and created automatically for every page request. What you need to do is the associate the object back to session using the lock function (demonstrated in the previous message). The lock should be done before you accessing the object.

Assume that you have an object retrieved from DB in the previous page and stored in a static hashtable, before you using it, you must associate it back to the session:


Object obj = hashtable.get(key);
XXPersistentManager.instance().getSession().lock(obj, LockModel.NONE);

// access your object
String val = obj.getX();
.....

in the above code sample, the XX will be your project name and the PersistentManager should be named as %Project name%PersistentManager.

If you have any further questions, please feel free to contact us again.

Best regards,
Rain

Does anyone know how to create a new session using the %app%PersistentManager generated with DBVA? I do NOT want to get the session but create a new one

Actually the %app%PersistentManager.getSession() is already returning a new session to you.

Best regards,
Rain

So if I have 2 different threads or web sessions calling getSession it will create 2 different sessions?

[quote=Rain]Actually the %app%PersistentManager.getSession() is already returning a new session to you.

Best regards,
Rain[/quote]

If the PersistentManager is running in different thread, then you will have 2 different ORM Session. In fact, you can use the user defined session type to get full control of the session. To use user defined session type:


PersisentManager.setSessionType(PersisentManager.SessionType.USER_DEFINED);

After that every call to PersistentManager.getSession() will return a new session. And you have to close it manually after use.

If you have any further questions, please feel free to contact us again.

Best regards,
Rain

Rain,
I am getting same exception while runnign some scheduled jobs on a server. This is a common server handling jobs for many projects, so I don’t have access to its web.xml. I am using hibernate-3.0.5

This error is being thrown on PRDUCTION server and I am not able to replicate on my DEV/QA server.

Please help me out to get this issue resolved.

Thanks…

Dear Payal,

Please make sure you are using the generated servlet filter, or you have closed the session at the end of every servlet/jsp.

Best Regards,
Jick