|
1 | 1 | # JPA mapping utilities for MapStruct
|
2 |
| - |
3 |
| -TODO: write readme file |
4 | 2 |
|
5 |
| -## Dependencies |
6 |
| - |
7 |
| - * Java >= 7 |
| 3 | +[](https://travis-ci.org/wavesoftware/java-mapstruct-jpa) [](https://sonar.wavesoftware.pl/dashboard/index/pl.wavesoftware.utils:mapstruct-jpa) [](https://coveralls.io/github/wavesoftware/java-mapstruct-jpa?branch=master) [](https://mvnrepository.com/artifact/pl.wavesoftware.utils/mapstruct-jpa) |
| 4 | + |
| 5 | +A set of utilities focused on mapping JPA managed entities with MapStruct. There are different utilities for different purposes and also a all-in-one utility for maximizing ease of use. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +* Domain model graph with cycles - via `CyclicGraphContext` |
| 10 | +* JPA aware mapping with update capability - via `JpaMappingContext` factory |
| 11 | +* [N+1 problem](https://stackoverflow.com/questions/97197/what-is-the-n1-select-query-issue) solution via special uninitialized collection classes, that throws exceptions if used |
| 12 | + |
| 13 | +### Domain model graph with cycles |
| 14 | + |
| 15 | +If you need to map a domain model with cycles in entity graph for ex.: (Pet.owner -> Person, Person.pets -> Pet) you can use a `CyclicGraphContext` as a MapStruct `@Context` |
| 16 | + |
| 17 | +```java |
| 18 | +@Mapper |
| 19 | +interface PetMapper { |
| 20 | + Pet map(PetData data, @Context CyclicGraphContext context); |
| 21 | + PetData map(Pet pet, @Context CyclicGraphContext context); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +### JPA aware mapping with update capability |
| 26 | + |
| 27 | +If you also need support for mapping JPA managed entities and be able to update them (not create new records) there more to be done. There is provided `JpaMappingContext` with factory. It requires couple more configuration to instantiate this context. |
| 28 | + |
| 29 | +`JpaMappingContext` factory requires: |
| 30 | +* Supplier of `StoringMappingContext` to handle cycles - `CyclicGraphContext` can be used here, |
| 31 | +* `Mappings` object that will provides mapping for given source and target class - mapping is information how to update existing object (managed entity) with data from source object, |
| 32 | +* `IdentifierCollector` should collect managed entity ID from source object |
| 33 | + |
| 34 | +The easiest way to setup all of this is to extend `AbstractJpaContextProvider`, implement `IdentifierCollector` and implement a set of `MappingProvider` for each type of entity. To provide implementations of `MappingProvider` you should create update methods in your MapStruct mappers. It utilize `CompositeContext` which can incorporate any number of contexts as a composite. |
| 35 | + |
| 36 | +All of this can be managed by some DI container like Spring or Guice. |
| 37 | + |
| 38 | +**Mapping facade as Spring service:** |
| 39 | + |
| 40 | +```java |
| 41 | +@Service |
| 42 | +@RequiredArgsConstructor |
| 43 | +final class MapperFacadeImpl implements MapperFacade { |
| 44 | + |
| 45 | + private final PetMapper petMapper; |
| 46 | + private final MapStructContextProvider<CompositeContext> contextProvider; |
| 47 | + |
| 48 | + @Override |
| 49 | + public PetJPA map(Pet pet) { |
| 50 | + return petMapper.map(pet, contextProvider.createNewContext()); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Pet map(PetJPA jpa) { |
| 55 | + return petMapper.map(jpa, contextProvider.createNewContext()); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +**Context provider as Spring service:** |
| 61 | + |
| 62 | +```java |
| 63 | +@Service |
| 64 | +@RequiredArgsConstructor |
| 65 | +final class CompositeContextProvider extends AbstractCompositeContextProvider { |
| 66 | + |
| 67 | + @Getter |
| 68 | + private final JpaMappingContextFactory jpaMappingContextFactory; |
| 69 | + private final List<MappingProvider<?, ?, ?>> mappingProviders; |
| 70 | + @Getter |
| 71 | + private final IdentifierCollector identifierCollector; |
| 72 | + |
| 73 | + @Override |
| 74 | + protected Iterable<MappingProvider> getMappingProviders() { |
| 75 | + return Collections.unmodifiableSet(mappingProviders); |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +**Example mapping provider for Pet as Spring service:** |
| 82 | + |
| 83 | +```java |
| 84 | +@Service |
| 85 | +@RequiredArgsConstructor |
| 86 | +final class PetMappingProvider implements MappingProvider<Pet, PetJPA, CompositeContext> { |
| 87 | + |
| 88 | + private final PetMapper petMapper; |
| 89 | + |
| 90 | + @Override |
| 91 | + public Mapping<Pet, PetJPA, CompositeContext> provide() { |
| 92 | + return AbstractCompositeContextMapping.mapperFor( |
| 93 | + Pet.class, PetJPA.class, |
| 94 | + petMapper::updateFromPet |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +**Identifier collector implementation as Spring service:** |
| 101 | + |
| 102 | +```java |
| 103 | +@Service |
| 104 | +final class IdentifierCollectorImpl implements IdentifierCollector { |
| 105 | + @Override |
| 106 | + public Optional<Object> getIdentifierFromSource(Object source) { |
| 107 | + if (source instanceof AbstractEntity) { |
| 108 | + AbstractEntity entity = AbstractEntity.class.cast(source); |
| 109 | + return Optional.ofNullable( |
| 110 | + entity.getReference() |
| 111 | + ); |
| 112 | + } |
| 113 | + return Optional.empty(); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +**HINT:** Complete working example in Spring can be seen in [coi-gov-pl/spring-clean-architecture hibernate module](https://github.com/coi-gov-pl/spring-clean-architecture/tree/develop/pets/persistence-hibernate/src/main/java/pl/gov/coi/cleanarchitecture/example/spring/pets/persistence/hibernate/mapper) |
| 119 | + |
| 120 | +**HINT:** An example for Guice can be seen in this repository in test packages. |
| 121 | + |
| 122 | +### N+1 problem solution via special uninitialized collection classes |
| 123 | + |
| 124 | +The N+1 problem is wide known and prominent problem when dealing with JPA witch utilizes lazy loading of data. Solution to this is that developers should fetch only data that they will need (for ex.: using `JOIN FETCH` in JPQL). In many cases that is not enough. It easy to slip some loop when dealing with couple of records. |
| 125 | + |
| 126 | +My solution is to detect that object is not loaded fully and provide a stub that will fail fast if data is not loaded and been tried to be used by other developer. To do that simple use `Uninitialized*` classes provided. There are `UninitializedList`, `UninitializedSet`, and `UninitializedMap`. |
| 127 | + |
| 128 | +```java |
| 129 | +@Mapper |
| 130 | +interface PetMapper { |
| 131 | + // [..] |
| 132 | + default List<Pet> petJPASetToPetList(Set<PetJPA> set, |
| 133 | + @Context CompositeContext context) { |
| 134 | + if (!Hibernate.isInitialized(set)) { |
| 135 | + return new UninitializedList<>(PetJPA.class); |
| 136 | + } |
| 137 | + return set.stream() |
| 138 | + .map(j -> map(j, context)) |
| 139 | + .collect(Collectors.toList()); |
| 140 | + } |
| 141 | + // [..] |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +**Disclaimer:** In future we plan to provide an automatic solution using dynamic proxy objects. |
| 146 | + |
| 147 | +## Dependencies |
| 148 | + |
| 149 | + * Java >= 8 |
| 150 | + * [MapStruct JDK8](https://github.com/mapstruct/mapstruct/tree/master/core-jdk8) >= 1.2.0 |
8 | 151 | * [EID Exceptions](https://github.com/wavesoftware/java-eid-exceptions) library
|
9 |
| - |
10 |
| -### Contributing |
11 |
| - |
| 152 | + |
| 153 | +### Contributing |
| 154 | + |
12 | 155 | Contributions are welcome!
|
13 |
| - |
| 156 | + |
14 | 157 | To contribute, follow the standard [git flow](http://danielkummer.github.io/git-flow-cheatsheet/) of:
|
15 |
| - |
| 158 | + |
16 | 159 | 1. Fork it
|
17 | 160 | 1. Create your feature branch (`git checkout -b feature/my-new-feature`)
|
18 | 161 | 1. Commit your changes (`git commit -am 'Add some feature'`)
|
19 | 162 | 1. Push to the branch (`git push origin feature/my-new-feature`)
|
20 | 163 | 1. Create new Pull Request
|
21 |
| - |
| 164 | + |
22 | 165 | Even if you can't contribute code, if you have an idea for an improvement please open an [issue](https://github.com/wavesoftware/java-mapstruct-jpa/issues).
|
0 commit comments