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

Best configuration of c3p0 [closed]

This is a configuration I am using which keeps resources to a minimum. Of course you’ll want to tailor your application to use the resources it needs… Reference: http://www.mchange.com/projects/c3p0/index.html testConnectionOnCheckin validates the connection when it is returned to the pool. testConnectionOnCheckOut, although would ensure active connections before use, would be too expensive to do. idleConnectionTestPeriod … Read more

Spring Data JPA: How can Query return Non- Entities Objects or List of Objects?

You can do something like @NamedQuery(name=”findWhatever”, query=”SELECT new path.to.dto.MyDto(e.id, e.otherProperty) FROM Student e WHERE e.id = ?1″) Then the MyDto object would just need a constructor defined with the correct fields i.e. public MyDto(String id, String otherProperty) { this.id = id; this.otherProperty = otherProperty; }

How to write Subqueries with In-Expressions in JPA 2.0, Criteria API?

Below is the pseudo-code for using sub-query using Criteria API. CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery(); Root<EMPLOYEE> from = criteriaQuery.from(EMPLOYEE.class); Path<Object> path = from.get(“compare_field”); // field to map with sub-query from.fetch(“name”); from.fetch(“id”); CriteriaQuery<Object> select = criteriaQuery.select(from); Subquery<PROJECT> subquery = criteriaQuery.subquery(PROJECT.class); Root fromProject = subquery.from(PROJECT.class); subquery.select(fromProject.get(“requiredColumnName”)); // field to map with main-query subquery.where(criteriaBuilder.and(criteriaBuilder.equal(“name”,name_value),criteriaBuilder.equal(“id”,id_value))); select.where(criteriaBuilder.in(path).value(subquery)); … Read more

How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6

Specification says that derived entity should be the owning side of the relationship: 2.4.1 Primary Keys Corresponding to Derived Identities The identity of an entity may be derived from the identity of another entity (the “parent” entity) when the former entity (the “dependent” entity) is the owner of a many-to-one or one-to-one relationship to the … Read more

Class not eligible for getting processed by all BeanPostProcessors

This warning means nothing, you shouldn’t worry about it since you don’t need to apply any post-processors to the DataSource. Technically it means that some bean post-processor (a transactional one, I guess) depends on your DataSource, therefore the DataSource must be fully initialized before initialization of that post-processor, so that the post-processor cannot intercept initialization … Read more

Java EE Architecture – Are DAO’s still recommended when using an ORM like JPA 2?

If I’m using an ORM like JPA2 – where I have my entities that are mapped to my database, should I still be using a DAO? It seems like a lot more overhead. It is. And clearly, Java EE doesn’t encourage using the DAO pattern when using JPA (JPA already provides a standardized implementation of … Read more