Skip to content

Instantly share code, notes, and snippets.

@davide-romanini
Created April 15, 2011 08:15
Show Gist options
  • Save davide-romanini/921371 to your computer and use it in GitHub Desktop.
Save davide-romanini/921371 to your computer and use it in GitHub Desktop.
CDI producer for spring-data-jpa based repositories
public interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person> {
List<Person> findByEmailAddressAndLastname(String emailAddress, String lastname);
// find all persons and apply eclipselink batch hint to fetch their addresses
@Query("select p from Person p")
@QueryHints({@QueryHint(name="eclipselink.batch", value="p.addresses")})
List<Person> findAllWithAddresses();
}
public class Repositories {
@PersistenceContext EntityManager em;
@Produces PersonRepository createPersonRepository() throws Exception {
// need to use getDelegate() in Glassfish, it should be safe anyway
JpaRepositoryFactory rf = new JpaRepositoryFactory((EntityManager)em.getDelegate());
return rf.getRepository(PersonRepository.class);
}
}
// now you can inject the generated proxy in your beans
@Model
public class PersonBean {
@Inject PersonRepository repo;
List<Person> personList;
@PostConstruct void init() {
this.personList = repo.findAllWithAddresses();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment