Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, <code>spring-data</code> does some extra magic that helps with complex queries. It is strange at first and you totally skip it in the docs but it is really powerful and useful.</p> <p>It involves creating a custom <code>Repository</code> and a custom `RepositoryImpl' and telling Spring where to find it. Here is an example:</p> <p>Configuration class - <s>point to your still-needed xml config</s> with annotation pointing to your repositories package (it looks for <code>*Impl</code> classes automatically now):</p> <pre><code>@Configuration @EnableJpaRepositories(basePackages = {"com.examples.repositories"}) @EnableTransactionManagement public class MyConfiguration { } </code></pre> <p><s>jpa-repositories.xml - tell <code>Spring</code> where to find your repositories. Also tell <code>Spring</code> to look for custom repositories with the <code>CustomImpl</code> file name:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt; &lt;jpa:repositories base-package="com.example.repositories" repository-impl-postfix="CustomImpl" /&gt; &lt;/beans&gt; </code></pre> <p></s></p> <p><code>MyObjectRepository</code> - this is where you can put annotated and unannotated query methods. Note how this repository interface extends the <code>Custom</code> one:</p> <pre><code>@Transactional public interface MyObjectRepository extends JpaRepository&lt;MyObject, Integer&gt;, MyObjectRepositoryCustom { List&lt;MyObject&gt; findByName(String name); @Query("select * from my_object where name = ?0 or middle_name = ?0") List&lt;MyObject&gt; findByFirstNameOrMiddleName(String name); } </code></pre> <p><code>MyObjectRepositoryCustom</code> - repository methods that are more complex and cannot be handled with a simple query or an annotation:</p> <pre><code>public interface MyObjectRepositoryCustom { List&lt;MyObject&gt; findByNameWithWeirdOrdering(String name); } </code></pre> <p><code>MyObjectRepositoryCustomImpl</code> - where you actually implement those methods with an autowired <code>EntityManager</code>:</p> <pre><code>public class MyObjectRepositoryCustomImpl implements MyObjectRepositoryCustom { @Autowired private EntityManager entityManager; public final List&lt;MyObject&gt; findByNameWithWeirdOrdering(String name) { Query query = query(where("name").is(name)); query.sort().on("whatever", Order.ASC); return entityManager.find(query, MyObject.class); } } </code></pre> <p>Amazingly, this all comes together and methods from both interfaces (and the CRUD interface, you implement) all show up when you do:</p> <pre><code>myObjectRepository. </code></pre> <p>You will see:</p> <pre><code>myObjectRepository.save() myObjectRepository.findAll() myObjectRepository.findByName() myObjectRepository.findByFirstNameOrMiddleName() myObjectRepository.findByNameWithWeirdOrdering() </code></pre> <p>It really does work. And you get one interface for querying. <code>spring-data</code> really is ready for a large application. And the more queries you can push into simple or annotation only the better off you are.</p> <p>All of this is documented at the <a href="http://docs.spring.io/spring-data/data-jpa/docs/1.4.x/reference/htmlsingle/#repositories.custom-implementations" rel="nofollow noreferrer">Spring Data Jpa site</a>.</p> <p>Good luck.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload