<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://luminoussheep.net/mediawiki/index.php?action=history&amp;feed=atom&amp;title=EJB_3</id>
	<title>EJB 3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://luminoussheep.net/mediawiki/index.php?action=history&amp;feed=atom&amp;title=EJB_3"/>
	<link rel="alternate" type="text/html" href="https://luminoussheep.net/mediawiki/index.php?title=EJB_3&amp;action=history"/>
	<updated>2026-04-16T20:32:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://luminoussheep.net/mediawiki/index.php?title=EJB_3&amp;diff=60&amp;oldid=prev</id>
		<title>Martin: Created page with &quot;* Containers support caching and coherence across containers, this leads to a scalable solution with little additional work * Performance problems are much reduced from the ea...&quot;</title>
		<link rel="alternate" type="text/html" href="https://luminoussheep.net/mediawiki/index.php?title=EJB_3&amp;diff=60&amp;oldid=prev"/>
		<updated>2021-09-14T21:28:41Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;* Containers support caching and coherence across containers, this leads to a scalable solution with little additional work * Performance problems are much reduced from the ea...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;* Containers support caching and coherence across containers, this leads to a scalable solution with little additional work&lt;br /&gt;
* Performance problems are much reduced from the early CMP&lt;br /&gt;
** still need to be careful about linking objects and activating too many&lt;br /&gt;
&lt;br /&gt;
= Hibernate =&lt;br /&gt;
* Used to require a XML mapping file&lt;br /&gt;
* Supports annotations much like JPA&lt;br /&gt;
* Tends to have additional features and push them into the JPA&lt;br /&gt;
* Supports one persistence unit per file - &lt;br /&gt;
* Entity classes must be defined - no auto discovery&lt;br /&gt;
* Includes Lucene search - full text&lt;br /&gt;
* Indexed collections&lt;br /&gt;
* Filters - similar to views but support parameters and dynamic application&lt;br /&gt;
* Shorthand HQL&lt;br /&gt;
* Manual flush + validator - user transactions&lt;br /&gt;
&lt;br /&gt;
= JPA =&lt;br /&gt;
* requires a persistence unit descriptor META-INF/persistence.xml&lt;br /&gt;
** specifying the datasource&lt;br /&gt;
** JPA specific configuration such as provider and it&amp;#039;s options&lt;br /&gt;
* one persistence unit per datasource&lt;br /&gt;
* annotated classes automatically discovered&lt;br /&gt;
* xml overrides annotations&lt;br /&gt;
* supports multiple persistence units in persistence.xml file&lt;br /&gt;
&lt;br /&gt;
== Property/Field ==&lt;br /&gt;
* public attributes are persisted using field based access i.e. direct access&lt;br /&gt;
* private attributes are persisted property based access i.e. using getter methods&lt;br /&gt;
* Can not mix field and property based access - can mark which by putting annotation against field or getter method&lt;br /&gt;
* Getters/setters can not be final&lt;br /&gt;
== Embeded ==&lt;br /&gt;
Object that doesn&amp;#039;t exist on it&amp;#039;s own outside of the object referring to it&lt;br /&gt;
* can not be persisted separately&lt;br /&gt;
&lt;br /&gt;
== Persistence Manager Factory ==&lt;br /&gt;
* Scans the classes for annotations&lt;br /&gt;
* Creates persistence manager for managing an entity instance&lt;br /&gt;
&lt;br /&gt;
== Persistence Manager ==&lt;br /&gt;
* Not thread safe - Nature of session beans this isn&amp;#039;t a problem&lt;br /&gt;
** stateful beans tied to one thread&lt;br /&gt;
** stateless beans only server one thread per call =&amp;gt; context per method&lt;br /&gt;
# manages entity - CRUD, findById, query. Manages state&lt;br /&gt;
## transient - created but not commited&lt;br /&gt;
## persisted - managed context&lt;br /&gt;
## detached - context closed or deleted&lt;br /&gt;
## removed - deleted&lt;br /&gt;
# persistence context - manage cache - queued sql, optimisations&lt;br /&gt;
# automated dirty checking - batched dirty cache flushing&lt;br /&gt;
# optimistic locking for long lived sessions&lt;br /&gt;
#* writes/locking delayed to the commit and locking/consistency checks performed then.&lt;br /&gt;
# can be configured to batch fetch when an iterator is accessed&lt;br /&gt;
# for a instance of the persistence manager and a persisted object multiple lookups guaranteed the same object instance&lt;br /&gt;
&lt;br /&gt;
* Allows traversing of relations - performs the lazy loading on demand&lt;br /&gt;
* Caches and manages updates until flushed then cascades any changes required&lt;br /&gt;
* Designed to serve a use case - stateful bean may (probably will want to) keep open across calls [[#Extended context]]&lt;br /&gt;
** stateful bean configured with @PersistenceContext(type = PersistenceContextType.EXTENDED)&lt;br /&gt;
&lt;br /&gt;
= JDO = &lt;br /&gt;
Java Data Objects&lt;br /&gt;
* JDO2 more coverage than JPA&lt;br /&gt;
* Separate persistence manager for transactions&lt;br /&gt;
* Intro http://www.ibm.com/developerworks/java/library/j-pj2ee4.html&lt;br /&gt;
* Comparison http://db.apache.org/jdo/jdo_v_jpa_orm.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Entity =&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import javax.persistence.Entity;&lt;br /&gt;
import javax.persistence.Id;&lt;br /&gt;
&lt;br /&gt;
@Entity&lt;br /&gt;
public class Foo implements Serializable {&lt;br /&gt;
@Id&lt;br /&gt;
private int id;&lt;br /&gt;
&lt;br /&gt;
public void setId(int id) {&lt;br /&gt;
  this.id = id;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int getId...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* relationships: @OneToOne @OneToMany @ManyToMany&lt;br /&gt;
** (mappedBy = &amp;quot;attributeName&amp;quot;, fetch = EAGER/LAZY, optional = true/false, cascade=true/false, targetEntity=ClassRequired.clazz)&lt;br /&gt;
*** mappedBy when the relationship points only one way - specify owner of relationship&lt;br /&gt;
**** not on ManyToOne - must be owner - no mappedBy&lt;br /&gt;
*** optional if related object is required&lt;br /&gt;
**** not available on OneToMany or ManyToMany&lt;br /&gt;
* mapping to tables: @Table @Column @JoinColumn&lt;br /&gt;
* not persisted @Transient&lt;br /&gt;
&lt;br /&gt;
http://db.apache.org/jdo/jdo_v_jpa.html&lt;br /&gt;
&lt;br /&gt;
Persisted types:&lt;br /&gt;
* primitive types, wrappers of primitives&lt;br /&gt;
* String, BigInteger, BigDecimal, java.util.Date, java.util.Calendar&lt;br /&gt;
* java.sql.Date, java.sql.Time, java.sql.Timestamp&lt;br /&gt;
* anything Serializable&lt;br /&gt;
* arrays (of things that can be persisted)&lt;br /&gt;
* Enums&lt;br /&gt;
* Collections (of things that can be persisted)&lt;br /&gt;
&lt;br /&gt;
= Entity Manger = &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  @PersistenceContext(unitName=&amp;quot;ComponetEntityManager&amp;quot;)&lt;br /&gt;
  private EntityManager em;&lt;br /&gt;
&lt;br /&gt;
  em.persist(foo);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= JPQL = &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import javax.persistence.EntityManager;&lt;br /&gt;
&lt;br /&gt;
  @PersistenceContext(unitName=&amp;quot;ComponetEntityManager&amp;quot;)&lt;br /&gt;
  private EntityManager entityManager;&lt;br /&gt;
&lt;br /&gt;
  Query q = entityManager.createQuery(&amp;quot;select t from Type t where t.foo = :foo&amp;quot;);&lt;br /&gt;
  q.setParameter(&amp;quot;foo&amp;quot;, bar);&lt;br /&gt;
  Type type =  (Type)q.getSingleResult( );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Definition BNF&lt;br /&gt;
** http://java.sun.com/javaee/5/docs/tutorial/backup/update3/doc/QueryLanguage5.html&lt;br /&gt;
* Examples on previous page:&lt;br /&gt;
** http://java.sun.com/javaee/5/docs/tutorial/backup/update3/doc/QueryLanguage4.html&lt;br /&gt;
&lt;br /&gt;
* updates entityManager.flush()&lt;br /&gt;
** entityManager.merge(type) issues:&lt;br /&gt;
*** loads another copy for comparison - extra read&lt;br /&gt;
*** may overwrite other changes - if detached&lt;br /&gt;
*** may hit non uniqueness issues and uninitialised associations&lt;br /&gt;
&lt;br /&gt;
= Intefaces =&lt;br /&gt;
Much simpler than EJB 2.0 - No longer have to implement any interfaces - though some still exist - see stateful&lt;br /&gt;
&lt;br /&gt;
Interface now only needs to include annotation @Remote or @Local&lt;br /&gt;
&lt;br /&gt;
* @Remote&lt;br /&gt;
* remote business interface&lt;br /&gt;
&lt;br /&gt;
= Local/Remote interface =&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import javax.ejb.Remote;&lt;br /&gt;
&lt;br /&gt;
@Remote&lt;br /&gt;
public interface FooBeanRemote {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Entity Beans =&lt;br /&gt;
* Just POJOs&lt;br /&gt;
* Needs to implement java.io.Serializable or Externalizable to be accessed remotely&lt;br /&gt;
* May throw javax.ejb.EJBException - extends java.lang.RuntimeException&lt;br /&gt;
* May declare exception behaviour using @javax.ejb.ApplicationException or &amp;lt;application-exception&amp;gt;&lt;br /&gt;
* May reference resources using annotations @Resource(name=&amp;quot;xxx&amp;quot;) rather than jndi lookup though InitialContext in code&lt;br /&gt;
* Much of the deployment descriptor is now available as annotations. Deployment descriptor can still be used to argument or override&lt;br /&gt;
** Note if full deployment descriptor used to define local, remote and session-type, bean could be devoid of any references to javax.ejb&lt;br /&gt;
&lt;br /&gt;
== Statelesss ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import javax.ejb.Stateless;&lt;br /&gt;
@Stateless&lt;br /&gt;
public class FooBean implements FooBeanRemote, FooBeanLocal {&lt;br /&gt;
@Resource(mappedName=&amp;quot;fooDB&amp;quot;)&lt;br /&gt;
DataSource ds;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
May also define local and remote interfaces with annotations&lt;br /&gt;
This prevents compile time checking of the interfaces though&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
@Stateless&lt;br /&gt;
@Local(FooBeanRemote.class)&lt;br /&gt;
@Remote(FooBeanLocal.class)&lt;br /&gt;
public class FooBean  {&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Stateful ==&lt;br /&gt;
* @Remove - annotates methods - releases bean from client when called&lt;br /&gt;
&lt;br /&gt;
=== Extended context ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import static javax.persistence.PersistenceContextType.EXTENDED;&lt;br /&gt;
...&lt;br /&gt;
@PersistenceContext(unitName=&amp;quot;ComponetEntityManager&amp;quot;, type=EXTENDED)&lt;br /&gt;
private EntityManager entityManager;&lt;br /&gt;
&lt;br /&gt;
... &lt;br /&gt;
 public void updateFoo(String high) {&lt;br /&gt;
     foo.setBar(high);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Entity manager continues to track changes to entity&lt;br /&gt;
* Updates to entity automatically get commited when the method completes - no need to call merge() or flush() &lt;br /&gt;
* Normal behaviour is to only track within method&lt;br /&gt;
==== Nesting ====&lt;br /&gt;
* When one stateful bean in nested in another the create and remove are cascaded to the nested bean automatically&lt;br /&gt;
* They can share the same persistence context if &lt;br /&gt;
** both are local&lt;br /&gt;
** both extended&lt;br /&gt;
** both use the same named context&lt;br /&gt;
&lt;br /&gt;
== Resources ==&lt;br /&gt;
* @Resource may also be used for any Enterprise Naming Context resource such as deployment descriptor references&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;ejb-jar&lt;br /&gt;
       xmlns=&amp;quot;http://java.sun.com/xml/ns/javaee&amp;quot;&lt;br /&gt;
       xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;br /&gt;
       xsi:schemaLocation=&amp;quot;http://java.sun.com/xml/ns/javaee&lt;br /&gt;
                           http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd&amp;quot;&lt;br /&gt;
       version=&amp;quot;3.0&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;enterprise-beans&amp;gt;&lt;br /&gt;
      &amp;lt;session&amp;gt;&lt;br /&gt;
        &amp;lt;ejb-name&amp;gt;FooBean&amp;lt;/ejb-name&amp;gt;&lt;br /&gt;
         &amp;lt;env-entry&amp;gt;&lt;br /&gt;
            &amp;lt;env-entry-name&amp;gt;bar&amp;lt;/env-entry-name&amp;gt;&lt;br /&gt;
            &amp;lt;env-entry-type&amp;gt;java.lang.String&amp;lt;/env-entry-type&amp;gt;&lt;br /&gt;
            &amp;lt;env-entry-value&amp;gt;ENC example&amp;lt;/env-entry-value&amp;gt;&lt;br /&gt;
         &amp;lt;/env-entry&amp;gt;&lt;br /&gt;
      &amp;lt;/session&amp;gt;&lt;br /&gt;
   &amp;lt;/enterprise-beans&amp;gt;&lt;br /&gt;
&amp;lt;/ejb-jar&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= EJB core classes = &lt;br /&gt;
== javax.ejb.SessionContext ==&lt;br /&gt;
[http://java.sun.com/javaee/5/docs/api/index.html?javax/ejb/SessionContext.html javax.ejb.SessionContext]&lt;br /&gt;
Now accessed with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Resource &lt;br /&gt;
javax.ejb.SessionContext ctx&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* getEJBObject() and getEJBLocalObject() - now obsolete and throw an exception&lt;br /&gt;
* &amp;lt;T&amp;gt; getBusinessObect(Class&amp;lt;T&amp;gt; businessInterface) - now returns the reference to current EJB to pass as a callback&lt;br /&gt;
** businessInterface - must be local or remote interface&lt;br /&gt;
** can not pass this reference to another bean&lt;br /&gt;
&lt;br /&gt;
* Class getInvolkedBusinessInterface() - returns local,remote or web service interface depending on which interface the current call was made&lt;br /&gt;
&lt;br /&gt;
== javax.ejb.EJBContext ==&lt;br /&gt;
SessionContext extends EJBContext&lt;br /&gt;
* Object lookup(String name) - Convince method&lt;br /&gt;
* TimerService getTimerService() - Gets a timerService to set up an event at a specific time/interval - also available as @Resource&lt;br /&gt;
* [http://java.sun.com/j2se/1.4.2/docs/api/java/security/Principal.html java.security.Principal] getCallerPrincipal() - can be used to get the name of the caller&lt;br /&gt;
* UserTransaction getUserTransaction() &lt;br /&gt;
&lt;br /&gt;
== javax.transaction.UserTransaction&lt;br /&gt;
* void begin()&lt;br /&gt;
* void commit()&lt;br /&gt;
* void rollback() - when complete no transaction is associated&lt;br /&gt;
* void setRollbackOnly() - marks transaction for rollback&lt;br /&gt;
* void setTransactionTimeout(int seconds)&lt;br /&gt;
* int getStatus()&lt;br /&gt;
&lt;br /&gt;
= Other technologies =&lt;br /&gt;
Lower level:&lt;br /&gt;
* JdbcTemplate http://www.vogella.de/articles/SpringJDBC/article.html&lt;br /&gt;
* iBATIS http://ibatis.apache.org/&lt;/div&gt;</summary>
		<author><name>Martin</name></author>
	</entry>
</feed>