What is Weld, JSR-299?

What does Weld do/give you? Weld is the reference implementation of the abstract JSR-299 API, which is better known as CDI, Contexts and Dependency Injection, an API which is provided through javax.enterprise.context and javax.enterprise.inject packages. How does it relate to Java EE 6? JSR-299 is part of Java EE 6 (JSR-316). How would one use … Read more

Can’t find javax.ws.rs package in jdk

These classes (JSR 311: JAX-RS: The JavaTM API for RESTful Web Services) are not part of the JDK. You need to include appropriate JAR file to your CLASSPATH. You can find the API e.g. in maven repository. Also check out apache-cxf, jersey (reference implementation), resteasy from JBoss, restlet and few other JAX-RS implementations.

Select MAX timestamp with JPA2 Criteria API

Instead of max one should use CriteriaBuilder.greatest for Timestamp (and for Date, String and other Comparables as well). And if you ever need MIN for Timestamp, then use least method instead Similar kind of issue can be be faced with less/greater/equal comparisons. Firs one accepts argument that extends Number, second one is for other comparables: … Read more

What is the difference between @Inject and @EJB

@EJB injects EJBs only, but @Inject can be used to inject POJOs rather than EJBs. However, @Inject requires that your archive be a BDA (contain beans.xml for EE 6, or implicitly in EE 7). @Inject also has additional CDI-specific capabilities (scopes, interceptors, etc.), but those capabilities incur extra overhead. Application servers have support for specifying … Read more

Testing an EJB with JUnit

The accepted answer requires mocking a lot of code, including the persistence layer. Use an embedded container to test the actual beans, instead; otherwise, mocking the persistence layer results in code that barely tests anything useful. Use a session bean with an entity manager that references a persistence unit: @Stateless public class CommentService { @PersistenceContext(unitName … Read more