-->
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.  [ 10 posts ] 
Author Message
 Post subject: Using entitymanager/transaction in j2se to test session bean
PostPosted: Sun Sep 25, 2005 9:31 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 10:29 am
Posts: 21
Hibernate version: hibernate-3.1beta3, hibernate-entitymanager-3.1beta3

Name and version of the database you are using: embedded hsqldb

I'm creating a small sample ejb3 project with 1 entity bean and 1 session bean.

Code:
@Entity
@Table(name="ledger_accounts")
public class LedgerAccount implements Serializable {

   private long id;
   private String description;
   
   public LedgerAccount() {
   }

   @Id(generate=GeneratorType.AUTO)
   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }

   @Column(name="description", nullable=false, length=50)
   public String getDescription() {
      return description;
   }

   public void setDescription(String description) {
      this.description = description;
   }
}


Code:
public @Stateless class LedgerServiceBean implements LedgerService {
   
   @PersistenceContext private EntityManager em;
   
   public void persistLedgerAccount(LedgerAccount ledgerAccount) {
      em.persist(ledgerAccount);
   }

   public void setEntityManager(EntityManager em) {
      this.em = em;
   }

}


If I understand correctly in a CMP environment this should be enough. The entity manager is injected and the container takes care of transaction behaviour.

I'm developing this sample outside a j2ee container and using junit tests to test the functionality. Because the session bean is a pojo I can instatiate it to test the functionality.
I can also create a new entity manager
Code:
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("reflection-rsa-em");
      EntityManager em = emf.createEntityManager();

and set it the session bean.

Because the container would take care of the transactions I get an exception that no transaction is in progress.

Code:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress
   at org.hibernate.ejb.AbstractEntityManagerImpl.checkTransactionActive(AbstractEntityManagerImpl.java:123)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:128)
   at reflection.rsa.services.LedgerServiceBean.persistLedgerAccount(LedgerServiceBean.java:32)
   at reflection.rsa.test.services.LedgerServiceTester.<init>(LedgerServiceTester.java:39)
   at reflection.rsa.test.services.LedgerServiceTester.main(LedgerServiceTester.java:57)


How can I deal best with this in a j2se environment?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 5:37 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
I have to do the following in my test code (without container) to test all my beans :

Code:
Transaction transaction = entityManager.getTransaction();
transaction.begin();
// use the beans here
...
...
transaction.commit();


Top
 Profile  
 
 Post subject: Considering using Spring Transaction Managent
PostPosted: Tue Sep 27, 2005 4:10 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 10:29 am
Posts: 21
Thanks,

I'm also considering using Spring Transaction Managent.
http://static.springframework.org/sprin ... ction.html

And to be more precisely: Declarative transaction management
http://static.springframework.org/sprin ... ml#d0e5690

In this way it should be really easy wrap a transaction around the session bean in a test (or local, not container) environment.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 3:34 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
You are on a very similar track like me. :) For testing just persistent objects I use the Entity Manager directly in the JUNIT test cases but when it comes to testing Services (which are build on top of the persistent objects) I use Spring to do the transaction management. Spring now also supports declaring transactions using Annotations (in addition to XML) but I haven't tried them yet. Also I've used Spring for transactions using the SessionFactory but haven't looked at if they support the new EJB3 entity manager.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Guys, you really should consider JBoss Embeddable EJB3. This is the perfec tool for your current problem.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: JBoss Seam
PostPosted: Fri Sep 30, 2005 5:13 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 10:29 am
Posts: 21
Hi emmanuel,

JBoss Embeddable EJB3 is just in alpha now. Nice thing about Spring is that's it really lightweight, and JBoss Embeddable is not!

However, I'm looking at JBoss SEAM and this really looks great with using EJB3 and JSF together. I also see great benefit for using JBoss Embeddable for testing etc.

But for a layered application with only using EJB3 entity and session beans (pojo's), I think Spring would benefit in a real lightweight solution without a J2EE container. You can deploy them also to any J2EE server without problems.

Regards,
Marcel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 5:36 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
How could you integration-test an EJB without an EJB container? Of course you can wrap your EJBs in Spring, but thats just useless if Spring doesn't run in an EJB container. Spring by itself does _nothing_, its just a trivial replacement for service lookup. Re-writing all your transaction assembly in proprietary Spring XML descriptors for its proprietary transaction interceptors is not going to buy you anything.

You can run the embeddable EJB3 container in any (or no) J2EE application server, that's really the point. You can deploy and test your EJBs without changes to the transaction assembly anywhere.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 6:04 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Since the only way to counter "lightweight" weblog myth that are now widespread believe are other weblogs, I'll blog about this later today and show in a few steps how easy and "lighweight" it is to run EJB3 beans everywhere.


Top
 Profile  
 
 Post subject: Re: JBoss Seam
PostPosted: Fri Sep 30, 2005 6:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
marceloverdijk wrote:
Nice thing about Spring is that's it really lightweight, and JBoss Embeddable is not!

But for a layered application with only using EJB3 entity and session beans (pojo's), I think Spring would benefit in a real lightweight solution without a J2EE container. You can deploy them also to any J2EE server without problems.


In this scenario, I see no weight difference between Embeddable EJB3 and Spring. Well Spring does not support JTA in standalone.
Embeddable EJB3 is an EJB3 container, not a J2EE container. Spring is a POJO container. EJB3 are POJOs. So Spring plus the needed extra libraries to make it JTA compliant is no different that Embeddable EJB3.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: JBoss Seam
PostPosted: Fri Sep 30, 2005 7:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
marceloverdijk wrote:
JBoss Embeddable EJB3 is just in alpha now. Nice thing about Spring is that's it really lightweight, and JBoss Embeddable is not!


Huh??? As the other guys pointed out this is a silly statement.

The only really plausible definitions I have seen of "lightweight" (one of the most overhyped and content-free notions in Java) are:

(1) Can run in a unit test or main method (ie. outside of monolithic WebLogic/WebSphere style J2EE container) and has a fast startup
(2) Has a user-friendly programming model based on JavaBeans/POJOs
(3) Is simple to use

Since Embeddable EJB3 passes all these tests, it is certainly "lightweight", assuming "lightweight" is actually intended to mean something concrete.


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

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.