Created
April 15, 2011 08:15
-
-
Save davide-romanini/921371 to your computer and use it in GitHub Desktop.
CDI producer for spring-data-jpa based repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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