I have an slsb in ejb3.0 with a number of methods, do I need to annotate each method with transaction attributes?
class decalaration:
@Stateless
@Local( { IMyLocalSLSB.class })
@Remote( { IMyRemoteSLSB.class }) })
public class mySLSB implements IMySLSB {
private EntityManager em;
and now some methods for inserting/updating and deleting db records are transactions annotations required, I thought they were not as jboss and EJB3.0 handle them using defaults:
public void crud(MyEntity myEntity) {
em.merge(myEntity);// inserts or updates
}
public void deleteInsert(MyEntity myEntity) {
MyEntity found = em.find(MyEntity.class, myEntity.getMyEntityPK());
if (found == null) {
em.merge(myEntity);
} else {
em.remove(found);
}
}
And finally, is this the best way to return a record/entity but not persist any further changes. I take a record and alter the data and then display (I could put into a DTO but this seems neater/less objects)
pu开发者_StackOverflow中文版blic MyEntity getAnEntity(integer id){
MyEntity myEntity = em.find(MyEntity.class, id);
org.hibernate.Session session = (Session) em.getDelegate();
session.evict(myEntity);
return myEntity;
}
To summarizes are annotation requried on methods for transactions in an slsb, and is session.evict the best way to stop any changes being persisted ?
Thanks!
To summarizes are annotation required on methods for transactions in an SLSB
No, they are not required. You can apply the @TransactionAttribute
annotation at the class-level to specify the default transaction attribute for all business methods of the enterprise bean. If you do not, your bean will default to @TransactionAttribute(REQUIRED)
(i.e. all methods are transacted by default).
and is
session.evict
the best way to stop any changes being persisted?
- JPA 1.0 has
EntityManager#clear()
but this would clear the whole persistence context.- If not wanted, then falling back to
Session#evict(Object)
is indeed the best option.
- If not wanted, then falling back to
- Unless you're using JPA 2.0 in which case there is
EntityManager#detach(Object)
now.
精彩评论