You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by angelb <ab...@yahoo.com> on 2013/05/22 09:47:19 UTC

Differences in persisting ForeignKeys between OpenJPA 2.1.0 and 2.2.1

I have the following 2 Entities

public class UsersStatusCodes implements Serializable {
      @Id
      @Column(name="USER_STATUS_CODE")
      private String userStatusCode;
      @Column(name="USER_STATUS_NAME")
      private String userStatusName;
      @OneToMany(mappedBy="intUserStatusCode")
      private Set<IntUsers> intUsersCollection;
     @OneToMany(mappedBy="statusCode")
     private Set<ExtUsers> extUsersCollection;
}

/*****/
public class ExtUsers implements Serializable {
      @Id
      @Column(name="EXT_USER_NAME")
      private String extUserName;
      @Column(name="FIRST_NAME")
      private String firstName;
      @Column(name="LAST_NAME")
      private String lastName;
      @Column(name="ADDRESS")
      private String address;
      @Column(name="ZIPCODE")
      private String zipcode;
     @ManyToOne
     @JoinColumn(name="EXT_USER_STATUS_CODE")
     private UsersStatusCodes statusCode;
}


In OpenJPA 2.1.0 I was able to persist ExtUsers object executing the
following code:

ExtUsers user=new ExtUsers();
user.setExtUserName(“aaa”);
user.setFirstName(“Fname”);
user.setLirstName(“Lname”);
/**StatusCode with Identity “ACTIVE” ALREADY EXISTS IN database, so there is
no need to persist it */
user.setStatusCode(new UsersStatusCodes(“ACTIVE”));
entityManager.persist(user); 
/*this was working perfectly*/

Now in OpenJPA 2.2.1 the abovementioned code generates the following
exception:

org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged
object "gr.bog.it.iris.model.jpa.UsersStatusCodes@4d48ee83" in life cycle
state  unmanaged while cascading persistence via field
"gr.bog.it.iris.model.jpa.ExtUsers.statusCode" during flush.  However, this
field does not allow cascade persist. You cannot flush unmanaged objects or
graphs that have persistent associations to unmanaged objects.
Suggested actions: a) Set the cascade attribute for this field to
CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or
"all" (JPA orm.xml), 
 b) enable cascade-persist globally,
c) manually persist the related field value prior to flushing.
d) if the reference belongs to another context, allow reference to it by
setting StoreContext.setAllowReferenceToSiblingContext().

What has change between the two OpenJPA versions?
Is it still possible to persist an entity without having to load all
references in advance using find()?

Thank you







--
View this message in context: http://openjpa.208410.n2.nabble.com/Differences-in-persisting-ForeignKeys-between-OpenJPA-2-1-0-and-2-2-1-tp7583990.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Differences in persisting ForeignKeys between OpenJPA 2.1.0 and 2.2.1

Posted by Rick Curtis <cu...@gmail.com>.
I suspect this change might have been introduced by OPENJPA-1986[1]. Try
setting the
property openjpa.Compatibility=CheckDatabaseForCascadePersistToDetachedEntity=true
to see if this gives you the old behavior.

Thanks,
Rick

[1] https://issues.apache.org/jira/browse/OPENJPA-1986


On Wed, May 22, 2013 at 2:47 AM, angelb <ab...@yahoo.com> wrote:

> I have the following 2 Entities
>
> public class UsersStatusCodes implements Serializable {
>       @Id
>       @Column(name="USER_STATUS_CODE")
>       private String userStatusCode;
>       @Column(name="USER_STATUS_NAME")
>       private String userStatusName;
>       @OneToMany(mappedBy="intUserStatusCode")
>       private Set<IntUsers> intUsersCollection;
>      @OneToMany(mappedBy="statusCode")
>      private Set<ExtUsers> extUsersCollection;
> }
>
> /*****/
> public class ExtUsers implements Serializable {
>       @Id
>       @Column(name="EXT_USER_NAME")
>       private String extUserName;
>       @Column(name="FIRST_NAME")
>       private String firstName;
>       @Column(name="LAST_NAME")
>       private String lastName;
>       @Column(name="ADDRESS")
>       private String address;
>       @Column(name="ZIPCODE")
>       private String zipcode;
>      @ManyToOne
>      @JoinColumn(name="EXT_USER_STATUS_CODE")
>      private UsersStatusCodes statusCode;
> }
>
>
> In OpenJPA 2.1.0 I was able to persist ExtUsers object executing the
> following code:
>
> ExtUsers user=new ExtUsers();
> user.setExtUserName(“aaa”);
> user.setFirstName(“Fname”);
> user.setLirstName(“Lname”);
> /**StatusCode with Identity “ACTIVE” ALREADY EXISTS IN database, so there
> is
> no need to persist it */
> user.setStatusCode(new UsersStatusCodes(“ACTIVE”));
> entityManager.persist(user);
> /*this was working perfectly*/
>
> Now in OpenJPA 2.2.1 the abovementioned code generates the following
> exception:
>
> org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged
> object "gr.bog.it.iris.model.jpa.UsersStatusCodes@4d48ee83" in life cycle
> state  unmanaged while cascading persistence via field
> "gr.bog.it.iris.model.jpa.ExtUsers.statusCode" during flush.  However, this
> field does not allow cascade persist. You cannot flush unmanaged objects or
> graphs that have persistent associations to unmanaged objects.
> Suggested actions: a) Set the cascade attribute for this field to
> CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or
> "all" (JPA orm.xml),
>  b) enable cascade-persist globally,
> c) manually persist the related field value prior to flushing.
> d) if the reference belongs to another context, allow reference to it by
> setting StoreContext.setAllowReferenceToSiblingContext().
>
> What has change between the two OpenJPA versions?
> Is it still possible to persist an entity without having to load all
> references in advance using find()?
>
> Thank you
>
>
>
>
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Differences-in-persisting-ForeignKeys-between-OpenJPA-2-1-0-and-2-2-1-tp7583990.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
*Rick Curtis*