You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@6degrees.com> on 2009/05/21 22:29:37 UTC
Refresh myself!
Hello,
I'm trying to create a convenient super class for my entities:
public abstract class JpaEntity implements Identifiable, Saveable,
Deletable, Loadable {
public void load() throws Exception { // Loadable
EntityManager em = ...
//em.merge(this); // uh, what?
em.refresh(this);
}
public void save() throws Exception { // Saveable
EntityManager em = ...
boolean alreadyInTransaction = em.getTransaction().isActive();
if (! alreadyInTransaction) em.getTransaction().begin();
if (getId() == null) em.persist(this); // Identifiable.getId()
else em.merge(this);
if (! alreadyInTransaction) em.getTransaction().commit();
}
public void delete() throws Exception { // Deleteable
EntityManager em = ...
boolean alreadyInTransaction = em.getTransaction().isActive();
if (! alreadyInTransaction) em.getTransaction().begin();
em.remove(em.merge(this));
if (! alreadyInTransaction) em.getTransaction().commit();
}
}
I hope it's obvious what I'm trying to do here. save() and delete() seem to
be working fine, but I don't know how to do load(). Basically, given a
detached instance that may have been modified, I want to reload it. Is it
possible for an instance to refresh itself, or do you have to reassign a new
instance to a variable (can't do this = em.merge(this), of course)? Am I
crazy, or is there some way to do this easily?
Thanks.
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com
Re: Refresh myself!
Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, May 21, 2009 at 5:10 PM, Pinaki Poddar <pp...@apache.org> wrote:
>
> Looks like you need a "copy constructor" in JpaEntity.
>
Hmm, I'm a little slow... could you elaborate?
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com
Re: Refresh myself!
Posted by Pinaki Poddar <pp...@apache.org>.
Hi daryl,
> Basically, given a detached instance that may have been modified, I want
to reload it.
Looks like you need a "copy constructor" in JpaEntity.
-----
Pinaki Poddar http://ppoddar.blogspot.com/
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
--
View this message in context: http://n2.nabble.com/Refresh-myself%21-tp2954027p2954214.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Refresh myself!
Posted by Daryl Stultz <da...@6degrees.com>.
On Fri, May 22, 2009 at 8:36 AM, Pinaki Poddar <pp...@apache.org> wrote:
>
> There is if you pierce the public API boundary. Try
>
Thanks for the tips, Pinaki. I think I'll hold off on my "Loadable" concept
and see if I really need it.
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com
Re: Refresh myself!
Posted by Pinaki Poddar <pp...@apache.org>.
> Ok, I get it. So there's no way to "reattach" an instance directly then?
There is if you pierce the public API boundary. Try
JPAFacadeHelper.toBroker(em).transactional(Object pc, boolean
updateVersion,
OpCallbacks call);
Daryl Stultz wrote:
>
> On Thu, May 21, 2009 at 8:50 PM, Pinaki Poddar <pp...@apache.org> wrote:
>
>>
>> Hi Daryl,
>> something like:
>> /**
>> * Pour the persistent state of the given other instance into this
>> (detached) instance.
>> * Some sort of reverse Object.clone().
>> * The implementation has to decide whether to deep copy or not.
>> **/
>
>
> Ok, I get it. So there's no way to "reattach" an instance directly then?
>
> PS: "copy constructor" is a constructor that takes an instance of the same
>> type as input argument. Was popular in C++ days.
>>
>> I'm afraid I missed that. I guess I'm too young. That's what I'll keep
> telling myself.
>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>
>
-----
Pinaki Poddar http://ppoddar.blogspot.com/
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
--
View this message in context: http://n2.nabble.com/Refresh-myself%21-tp2954027p2957118.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Refresh myself!
Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, May 21, 2009 at 8:50 PM, Pinaki Poddar <pp...@apache.org> wrote:
>
> Hi Daryl,
> something like:
> /**
> * Pour the persistent state of the given other instance into this
> (detached) instance.
> * Some sort of reverse Object.clone().
> * The implementation has to decide whether to deep copy or not.
> **/
Ok, I get it. So there's no way to "reattach" an instance directly then?
PS: "copy constructor" is a constructor that takes an instance of the same
> type as input argument. Was popular in C++ days.
>
> I'm afraid I missed that. I guess I'm too young. That's what I'll keep
telling myself.
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com
Re: Refresh myself!
Posted by Pinaki Poddar <pp...@apache.org>.
Hi Daryl,
something like:
/**
* Pour the persistent state of the given other instance into this
(detached) instance.
* Some sort of reverse Object.clone().
* The implementation has to decide whether to deep copy or not.
**/
abstract void enolc(JpaEntity other);
public void load() throws Exception { // Saveable
EntityManager em = ...
this.enolc(em.find(getClass(), getId());
}
PS: "copy constructor" is a constructor that takes an instance of the same
type as input argument. Was popular in C++ days.
-----
Pinaki Poddar http://ppoddar.blogspot.com/
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
--
View this message in context: http://n2.nabble.com/Refresh-myself%21-tp2954027p2954998.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.