Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation Validation Exception (com.ibm.wsspi.amm.validate.ValidationException)

I got following exception on IBM Websphere Application server 8.5 when I tried to deploy my application on server. I'm using WS 8.5, EJB 3.1, Java EE 6 in my application.

[11/1/12 11:06:47:208 PKT] 0000005d annotations   E   CWWAM0003E: An exception occurred while validating an annotation: com.ibm.wsspi.amm.validate.ValidationException: CWWAM2302E: The class com.xxx.yyy.services.UsersServiceBean is annotated with an invalid @PersistenceContext declaration; no name is specified.
                             com.ibm.wsspi.amm.validate.ValidationException: CWWAM2302E: The class com.xxx.yyy.services.UsersServiceBean is annotated with an invalid @PersistenceContext declaration; no name is specified.
at com.ibm.ws.amm.validate.persistence.PersistenceContextValidator.validateClassAnnotation(PersistenceContextValidator.java:86)

Below is my Java code.

@PersistenceContext(unitName="myUnit")
@Stateless(name="UsersService")
public class UsersServiceBean implements UsersService {...}
like image 642
Sibtain Norain Avatar asked Feb 17 '26 03:02

Sibtain Norain


1 Answers

In short, when declaring @PersistenceUnit on a class, you must add a name="..." that you can use to look up the EntityManagerFactory with new InitialContext("java:..."). Alternatively, you can declare an @PersistenceUnit(...) EntityManagerFactory emf; field in your class and omit the name.

All ref annotations are basically the same as @Resource. Per the commons annotations specification:

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.

The last sentence is relevant, and it makes sense: using @PersistenceUnit (and all other @Resource-like annotations) has two effects:

  1. If you declare the annotation on a field or method, then the container will automatically inject when the instance is created.
  2. The reference is inserted into the java:comp namespace using its name. If you declare the annotation on a field or method, the default name is java:comp/env/com.example.ClassName/targetName.

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 java:comp/env. In that case, the annotation declaration would be pointless, so it's an error.

like image 96
Brett Kail Avatar answered Feb 21 '26 15:02

Brett Kail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!