|
| 1 | +# Domain Module |
| 2 | + |
| 3 | +The domain module contains the domain model object and the Spring-Data-JPA Repositories for accessing the database. In order to |
| 4 | +compile correctly, you have to enable the JPA Metamodel generator. It is an annotation processor and the instructions for enabling |
| 5 | +it in your IDE are available at: http://docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/#whatisit |
| 6 | + |
| 7 | +Maven already has it enabled, so if you don't want to work on this package, you only need to run the maven build and the |
| 8 | +required files will be generated. |
| 9 | + |
| 10 | +## Structure and Conventions |
| 11 | + |
| 12 | +The package structure of this module is to put: |
| 13 | + |
| 14 | +* JPA Entity and related Embeddable classes in the org.fao.geonet.domain package or sub-packages |
| 15 | +* JPA EntityListener objects in org.fao.geonet.entitylistener package or sub-packages |
| 16 | +* Spring-data repository objects in org.fao.geonet.repository package or sub-packages |
| 17 | +* Spring-data-jpa Specification Utility/Factory objects in org.fao.geonet.repository.specification package or sub-packages |
| 18 | + |
| 19 | +The file config-spring-geonetwork.xml in the src/main/resources directory configures spring and JPA to scan the |
| 20 | +org.fao.geonet.domain package (and sub-packages) for Entities and the org.fao.geonet.repository package for spring-data-jpa |
| 21 | +Repository objects. |
| 22 | + |
| 23 | +The exact list that is scanned by the spring configuration files needs to be checked. At the time of this writing the file |
| 24 | +is config-spring-geonetwork.xml in this module. |
| 25 | + |
| 26 | +The side effect of this means that repositories and entities can be in other modules as well. At the time of this writing |
| 27 | +that is not the case but should be considered in the future. |
| 28 | + |
| 29 | +## Writing Custom Queries |
| 30 | + |
| 31 | +IMPORTANT: Please do not add any Specifications or Queries to the system without at least one test for each query! |
| 32 | + |
| 33 | +Spring Data allows four different ways to write queries. (For more in-depth explanation see: http://projects.spring.io/spring-data-jpa/ |
| 34 | + |
| 35 | +1. Query methods |
| 36 | + - You can create methods in the ...Repository interface and the method name will be parsed by Spring Data and a query will be created for you based on the name. There are many different options including in, and, or ways to write the methods. |
| 37 | + - While the methods are easy to write, they are not very flexible. Using Specifications tends to be a more flexible solution and is therefore preferred. |
| 38 | + - **Examples: (See tests for example usage)** |
| 39 | + * [UserRepository](src/main/java/org/fao/geonet/repository/UserRepository.java) |
| 40 | + * [OperationAllowedRepository](src/main/java/org/fao/geonet/repository/OperationAllowedRepository.java) |
| 41 | +2. Specifications |
| 42 | + - The Specification interface provides a composable API for writing queries. |
| 43 | + - For example, you could write a hasMetadataId specification and a hasOwnerId specification. One could then compose them using not, and, or. |
| 44 | + - Each specification should be in the appropriate <DomainObject>Specs class. |
| 45 | + - Each specification is to be created with a static method. |
| 46 | + - Each specification must have a test. |
| 47 | + - **Examples: (See tests for example usage)** |
| 48 | + * [MetadataSpecs](src/main/java/org/fao/geonet/repository/specification/MetadataSpecs.java) |
| 49 | + * [UserSpecs](src/main/java/org/fao/geonet/repository/specification/UserSpecs.java) |
| 50 | +3. Custom Queries |
| 51 | + - Spring JPA Repositories can have custom queries with very custom implementations. A Repository will extend an interface with the custom methods and the implementations would be in a subclass of the interface. |
| 52 | + - The implementation will use the normal EntityManager (JPA) API for constructing the queries. |
| 53 | + - **Examples: (See tests for example usage)** |
| 54 | + * [MetadataRepositoryCustom](src/main/java/org/fao/geonet/repository/MetadataRepositoryCustom.java) |
| 55 | + * [MetadataRepositoryCustomImpl](src/main/java/org/fao/geonet/repository/MetadataRepositoryCustomImpl.java) |
| 56 | +4. Standard JPA querying |
| 57 | + - One can obtain an EntityManager by using the @PersistentContext annotation on a field in a Spring bean. |
| 58 | + - The EntityManager can be used to write queries. |
| 59 | + - All Queries should be contained in the same module as the domain object that it is querying, unless the module is a plugin module. |
| 60 | + - **Examples: (See tests for example usage)** |
| 61 | + * [MetadataReportsQueries](src/main/java/org/fao/geonet/repository/reports/MetadataReportsQueries.java) |
0 commit comments