https://github.com/sbespalov/cs-mapper
Useful POJO mapper
https://github.com/sbespalov/cs-mapper
beanmapper dozer dozermapper dto entity jpa-entities jpa-entity-model plane-pojos pojo spring
Last synced: 16 days ago
JSON representation
Useful POJO mapper
- Host: GitHub
- URL: https://github.com/sbespalov/cs-mapper
- Owner: sbespalov
- Created: 2017-04-04T01:02:32.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-07T09:58:33.000Z (over 8 years ago)
- Last Synced: 2025-11-30T15:09:26.308Z (6 months ago)
- Topics: beanmapper, dozer, dozermapper, dto, entity, jpa-entities, jpa-entity-model, plane-pojos, pojo, spring
- Language: Java
- Size: 271 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Introdution
There is a common view, that the **_DTO (Data Transfer Object)_** is an anti-pattern, but **_CSMapper_** targets to dispel this prejudice.
If we imagine a standard **_JavaEE_** application, then, as a rule, it has a "Layered architecture" with an API on top (which is **_Presensation Layer_**), and JPA domain model at the bottom (which is **_Persistence Layer_**). Also we can say that API parameters depends on **_JPA Entity Model_** at most, and is a kind of its projection. So here is the dummy copy paste come from, which is the reason that makes DTO called ~~antipattern~~. But this copy paste problem can be very simply solved, which is what the **_CSMapper_** makes, and turn the DTO back into a very cool pattern.
We can divide the solution into three parts:
* generate plane POJOs, based on JPA Entity model;
* construct API parameters with those plane POJOs;
* merge values from Entity to POJO and vice versa;
That's it!
There are usage examples below, which will help to make some details clearer.
# Usage
All features, that **_CSMapper_** provides, can be used out of the box with well known Java technology stack, such as Spring and Hibernate. The configuration is very simple and takes about 15 minutes:
#### 1. Generate **_JPA Entity_** based **_POJOs_**
The code generation process occurs during the project build. This is done using the Maven plugin:
```xml
org.apache.maven.plugins
maven-javadoc-plugin
domaindto
generate-sources
javadoc
org.carlspring.beans.utils.doclet.DomainDtoDoclet
org.carlspring.beans.mapper.examples.domain
org.carlspring.beans
cs-mapper-utils
1.0
-basepackage
org.carlspring.beans.mapper.examples.domain
-outputfolder
${project.build.directory}/generated-sources
-prefexedproperties
id,name
false
512
org.codehaus.mojo
build-helper-maven-plugin
add-source
generate-sources
add-source
${project.build.directory}/generated-sources
```
> Note that this configuration must be placed in module, that contains JPA Entities
We using the `DomainDtoDoclet` to get **_JPA Entities_** metadata from `.java` sources. Also there will be a dedicated `cs-mapper-plugin` instead of `maven-javadoc-plugin` and `build-helper-maven-plugin` in future versions.
#### 2. **_BeanMapper_** instace configuration
**_BeanMapper_** can be easily configured inside **_Spring Application Context_** using **_BeanMapperFactoryBean_** like this:
```java
@Configuration
public class AppConfiguration {
...
@Bean
public BeanMapperFactoryBean beanMapper(EntityManagerFactory emf)
{
BeanMapperFactoryBean result = new BeanMapperFactoryBean(emf);
result.setMappedClasses(Arrays.asList(DomainDtoClasses.domainDtoClasses));
result.setPackagesToScan("your.application.package.name.api.dto");
return result;
}
}
```
#### 3. Merge POJOs with **_JPA Entities_** with **_BeanMapper_**
```java
@Component
public class SampleApiService {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private BeanMapper beanMapper;
public PetStoreReadDto getPetStore(Long petStoreId) {
PetStoreEntity petStoreEntity = entityManager.find(PetStoreEntity.class, petStoreId);
return (PetStoreReadDto) beanMapper.convertObject(petStoreEntity, PetStoreReadDto.class);
}
}
```
## Common Use Cases
Usage examples can be found [here](https://github.com/sbespalov/cs-mapper/tree/master/cs-mapper-examples)