0

Fist of all, I'm using EclipseLink 2.5.2, ojdbc6, spring-orm 4.1.1 and QueryDSL 3.7.1.

I don't understand why my objects are not in my persistence context (or is this how it should be?). I'm using QueryDSL to query my objects, however when I try to persist such an object using entitymanager.persist() it always creates an insert statement resulting in in a duplicated primarykey exception. Calling refresh() on the object crashes with java.lang.IllegalArgumentException: Can not refresh not managed object. Using merge() works fine however that's not what I want. I need to keep my original reference to the saved object.

persistence.xml

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="XXXXXX"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/XXXXX</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.weaving" value="static" />
            <property name="eclipselink.target-database" value="Oracle11" />
        </properties>
    </persistence-unit>
</persistence>

The entitymanager used to create the JPAQuery and to refresh/merge/persist are the same.

If you need more information/configurations/etc. please leave a comment. I'm really stuck and can't wrap my head around what the reason could be and what other information could be useful to you guys.

martinyyyy
  • 1,652
  • 3
  • 21
  • 44
  • are you trying to persist the object including the id? which im guessing is the pk – Gregorio Meraz Jr. Mar 08 '16 at 19:14
  • 1
    Don't describe your code. Post it. There's no reason to call persist() on an object that you got from a query. persist is used to insert new entities. If you got them from a query, then they aren't new. – JB Nizet Mar 08 '16 at 19:17
  • cant help whitout full classes but im gonna guess the issue is that you are trying to persist an object that already exists on the database, thats why you are getting the primary key exception, of course the merge method is going to work since its like an update method... – Gregorio Meraz Jr. Mar 08 '16 at 19:30
  • yeah. that's it. I was reading around stackoverflow like http://stackoverflow.com/questions/1069992/jpa-entitymanager-why-use-persist-over-merge and thought persist is basically the same as merge but better. – martinyyyy Mar 09 '16 at 08:20

1 Answers1

1

EntityManager.persist() is used to make a transient instance persistent. In this case transient (a term used by Hibernate, but valid for other persistence providers as well) means an entity which doesn't have a representation in the persistence context or the underlying datastore. It's not meant to be used on entities already present in the database. Use merge() to update persistent entities.

There is an article about the subject with a nice state diagram representing the states an entity can be in and the transitions between those states: Entity states

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47