Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In short, when declaring <code>@PersistenceUnit</code> on a class, you must add a <code>name="..."</code> that you can use to look up the <code>EntityManagerFactory</code> with <code>new InitialContext("java:...")</code>. Alternatively, you can declare an <code>@PersistenceUnit(...) EntityManagerFactory emf;</code> field in your class and omit the name.</p> <p>All ref annotations are basically the same as <code>@Resource</code>. Per the commons annotations specification:</p> <blockquote> <p>The name element is the JNDI name of the resource. When the Resource annotation is applied on a field, the default value of the name element is the field name qualified by the class name. When applied on a method, the default is the JavaBeans property name corresponding to the method qualified by the class name. When applied on a class, there is no default and the name MUST be specified.</p> </blockquote> <p>The last sentence is relevant, and it makes sense: using <code>@PersistenceUnit</code> (and all other <code>@Resource</code>-like annotations) has two effects:</p> <ol> <li>If you declare the annotation on a field or method, then the container will automatically inject when the instance is created.</li> <li>The reference is inserted into the <code>java:comp</code> namespace using its name. If you declare the annotation on a field or method, the default name is <code>java:comp/env/com.example.ClassName/targetName</code>.</li> </ol> <p>If you declare the annotation on a class, then (1) there's no injection, and (2) there's no default name so there's no way to bind into <code>java:comp/env</code>. In that case, the annotation declaration would be pointless, so it's an error.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload