These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: CallBack Methods messing with EntityManager and Transaction
PostPosted: Mon Mar 10, 2008 6:56 am 
Newbie

Joined: Tue Mar 04, 2008 10:00 am
Posts: 2
Location: italy
I have this problem and your help would be rellay appreciated ;
I need to do some NamedQueries execution INSIDE a method marked
as @PostPersist . I've read here and there that queries and entityManager
should not be involved in callback methods , like this link explain :
http://www.hibernate.org/hib_docs/entit ... eners.html

is this really so ? How can i persist manually something if i want to manually
persist it JUST in the moment which another particular object is persisted ?

My code is below ...

@NamedQuery(name = "brokers.insert", query = "INSERT INTO Broker (master_id,detail_id,type) SELECT b.master_id,b.detail_id,b.type FROM
Broker b WHERE (b.master_id=?2 AND b.detail_id=?3 AND b.type=?4)"),


@PostPersist
private void writeBrokers()
{
EntityManager em = JPAContext.entityManager();
EntityTransaction transazione = em.getTransaction();

List<Broker> brokers = getBrokersListForThisIdentifiable();

deleteBrokersGivenMasterId(em, brokers);
insertBrokersGivenMasterId(em, brokers);
}

private void insertBrokersGivenMasterId(EntityManager em, List<Broker> brokers)
{
Query insertBrokersGivenMasterId = em.createNamedQuery("brokers.insert");

for (int i = 0; i < brokers.size(); i++)
{
Broker broker = brokers.get(i);
Integer master_id = broker.getMasterId();

insertBrokersGivenMasterId.setParameter(2, broker.getMasterId());
insertBrokersGivenMasterId.setParameter(3, broker.getDetailId());
insertBrokersGivenMasterId.setParameter(4, broker.getType());

if(master_id == this.id)
insertBrokersGivenMasterId.executeUpdate();
}
}

private void deleteBrokersGivenMasterId(EntityManager em,List<Broker> brokers)
{

Query deleteBrokersGivenMasterId = em.createNamedQuery("brokers.delete");

for (int i = 0; i < brokers.size(); i++)
{
Broker broker = brokers.get(i);

deleteBrokersGivenMasterId.setParameter(1, broker.getMasterId());

if(broker.getMasterId() == this.id)
deleteBrokersGivenMasterId.executeUpdate();
}
}
}

i need to persist some object only in some cases ... and i don't want this specific case handled by jpa , i wish to handle this happening by myself...
i would be greatful for any advice , thank you a lot

_________________
Marijuana causes amnesia and other things I don't remember.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.