So I'm trying for the first time in a not so complex project to implement Domain Driven Design by separating all my code into application, domain, infrastructure and interfaces packages.
I also went with the whole separation of the JPA Entities to Domain models that will hold my business logic as rich models and used the Builder pattern to instantiate. This approach created me a headache and can't figure out if Im doing it all wrong when using JPA + ORM and Spring Data with DDD.
Process explanation
The application is a Rest API consumer (without any user interaction) that process daily through Scheduler tasks a fairly big amount of data resources and stores or updates into MySQL. Im using RestTemplate to fetch and convert the JSON responses into Domain objects and from there Im applying any business logic within the Domain itself e.g. validation, events, etc
From what I have read the aggregate root object should have an identity in their whole lifecycle and should be unique. I have used the id of the rest API object because is already something that I use to identify and track in my business domain. I have also created a property for the Technical id so when I convert Entities to Domain objects it can hold a reference for the update process.
When I need to persist the Domain to the data source (MySQL) for the first time Im converting them into Entity objects and I persist them using the save() method. So far so good.
Now when I need to update those records in the data source I first fetch them as a List of Employees from data source, convert Entity objects to Domain objects and then I fetch the list of Employees from the rest API as Domain models. Up until now I have two lists of the same Domain object types as List<Employee>. I'm iterating them using Streams and checking if an objects are not equal() between them if yes a collection of List items is created as a third list with Employee objects that need to be updated. Here I've already passed the technical Id to the domain objects in the third list of Employees so Hibernate can identify and use to update the records that are already exists.
Up to here are all fairly simple stuff until I use the saveAll() method to update the records.
Questions
I alway see Hibernate using INSERT instead of updating the list of
records. So If Im correct Hibernate session is not recognising the
objects that Im throwing into it because I have detached them when I
used the convert to domain object?
Does anyone have a better idea how can I implement this differently or fix
this problem?
Or should I stop using this approach as two different objects and continue use
them as rich Entity models?
Simple classes to explain it with code
EmployeeDO.java
#Entity
#Table(name = "employees")
public class EmployeeDO implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public EmployeeDO() {}
...omitted getter/setters
}
Employee.java
public class Employee {
private Long persistId;
private Long employeeId;
private String name;
private Employee() {}
...omitted getters and Builder
}
EmployeeConverter.java
public class EmployeeConverter {
public static EmployeeDO serialize(Employee employee) {
EmployeeDO target = new EmployeeDO();
if (employee.getPersistId() != null) {
target.setId(employee.getPersistId());
}
target.setName(employee.getName());
return target;
}
public static Employee deserialize(EmployeeDO employee) {
return new Country.Builder(employee.getEmployeeId)
.withPersistId(employee.getId()) //<-- Technical ID setter
.withName(employee.getName())
.build();
}
}
EmployeeRepository.java
#Component
public class EmployeeReporistoryImpl implements EmployeeRepository {
#Autowired
EmployeeJpaRepository db;
#Override
public List<Employee> findAll() {
return db.findAll().stream()
.map(employee -> EmployeeConverter.deserialize(employee))
.collect(Collectors.toList());
}
#Override
public void saveAll(List<Employee> employees) {
db.saveAll(employees.stream()
.map(employee -> EmployeeConverter.serialize(employee))
.collect(Collectors.toList()));
}
}
EmployeeJpaRepository.java
#Repository
public interface EmployeeJpaRepository extends JpaRepository<EmployeeDO, Long> {
}
I use the same approach on my project: two different models for the domain and the persistence.
First, I would suggest you to don't use the converter approach but use the Memento pattern. Your domain entity exports a memento object and it could be restored from the same object. Yes, the domain has 2 functions that aren't related to the domain (they exist just to supply a non-functional requirement), but, on the other side, you avoid to expose functions, getters and constructors that the domain business logic never use.
For the part about the persistence, I don't use JPA exactly for this reason: you have to write a lot of code to reload, update and persist the entities correctly. I write directly SQL code: I can write and test it fast, and once it works I'm sure that it does what I want. With the Memento object I can have directly what I will use in the insert/update query, and I avoid myself a lot of headaches about the JPA of handling complex tables structures.
Anyway, if you want to use JPA, the only solution is to:
load the persistence entities and transform them into domain entities
update the domain entities according to the changes that you have to do in your domain
save the domain entities, that means:
reload the persistence entities
change, or create if there're new ones, them with the changes that you get from the updated domain entities
save the persistence entities
I've tried a mixed solution, where the domain entities are extended by the persistence ones (a bit complex to do). A lot of care should be took to avoid that domain model should adapts to the restrictions of JPA that come from the persistence model.
Here there's an interesting reading about the splitting of the two models.
Finally, my suggestion is to think how complex the domain is and use the simplest solution for the problem:
is it big and with a lot of complex behaviours? Is expected that it will grow up in a big one? Use two models, domain and persistence, and manage the persistence directly with SQL It avoids a lot of caos in the read/update/save phase.
is it simple? Then, first, should I use the DDD approach? If really yes, I would let the JPA annotations to split inside the domain. Yes, it's not pure DDD, but we live in the real world and the time to do something simple in the pure way should not be some orders of magnitude bigger that the the time I need to to it with some compromises. And, on the other side, I can write all this stuff in an XML in the infrastructure layer, avoiding to clutter the domain with it. As it's done in the spring DDD sample here.
When you want to update an existing object, you first have to load it through entityManager.find() and apply the changes on that object or use entityManager.merge since you are working with detached entities.
Anyway, modelling rich domain models based on JPA is the perfect use case for Blaze-Persistence Entity Views.
Blaze-Persistence is a query builder on top of JPA which supports many of the advanced DBMS features on top of the JPA model. I created Entity Views on top of it to allow easy mapping between JPA models and custom interface defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure the way you like and map attributes(getters) via JPQL expressions to the entity model. Since the attribute name is used as default mapping, you mostly don't need explicit mappings as 80% of the use cases is to have DTOs that are a subset of the entity model.
The interesting point here is that entity views can also be updatable and support automatic translation back to the entity/DB model.
A mapping for your model could look as simple as the following
#EntityView(EmployeeDO.class)
#UpdatableEntityView
interface Employee {
#IdMapping("persistId")
Long getId();
Long getEmployeeId();
String getName();
void setName(String name);
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
Employee dto = entityViewManager.find(entityManager, Employee.class, id);
The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features and it can also be saved back. Here a sample repository
#Repository
interface EmployeeRepository {
Employee findOne(Long id);
void save(Employee e);
}
It will only fetch the mappings that you tell it to fetch and also only update the state that you make updatable through setters.
With the Jackson integration you can deserialize your payload onto a loaded entity view or you can avoid loading alltogether and use the Spring MVC integration to capture just the state that was transferred and flush that. This could look like the following:
#RequestMapping(path = "/employee/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateEmp(#EntityViewId("id") #RequestBody Employee emp) {
employeeRepository.save(emp);
return ResponseEntity.ok(emp.getId().toString());
}
Here you can see an example project: https://github.com/Blazebit/blaze-persistence/tree/master/examples/spring-data-webmvc
Related
I am developing a rest application where the data in DB is loaded in Entities then some transformations are made on the data while being filled in corresponding DTOs then returned back to the consumer.
According to the consumer and some other parameters, a different subset of the data should be returned to the user, for example if user is inquiring on his personal info, level of details returning will be different than if a manager is inquiring on the data of his employees, etc ...
My question:
Is there any framework to handle this custom mapping (i.e. an xml based file that determines which field in which BE Entity should be mapped to which DTO in which condition ? instead of making a custom code in each case? thanks in advance.
I am using spring rest + hibernate
About XML file mapping, I do not know any. But what I find really useful and very customizable is MapStruct. It is a very useful library and the docs and examples are very good.
A simple example:
#Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
#Mapping(source = "numberOfSeats", target = "seatCount") // Here is one of the functionalities that you wanted...
CarDto carToCarDto(Car car);
}
And there is IDE and Lombok support also.
I hope that someone might have had this or a similar problem before and could help me :)
I have a KryoNet Server / Client architecture, where I am sending messages.
One of these Messages contains an instance of a class "WorldEntity".
This entity looks something like this:
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class WorldEntity {
#OneToMany
private Collection<EntityComponent> components;
}
I am using spring + hibernate to save this entity to my postgresql database.
This works all fine.
When I am now sending this entity over to the client, the kryonet serializer tries to load the components lazily, which it should not do, since there is no hibernate or database on the client.
In fact, all data is already contained in the entity.
I read somewhere, that a custom Serialization Object could be created and added to the KryoNet client, to disable this hibernate loading:
Kryo kryoSerializer = new Kryo() {
#Override
public Serializer<?> getDefaultSerializer(final Class type) {
if (AbstractPersistentCollection.class.isAssignableFrom(type)) {
return new FieldSerializer(kryoSerializer, type);
}
return super.getDefaultSerializer(type);
}
};
This, unfortunately, can not be used as a Serialization object in the constructor of the Kryo Client.
Any help will be greatly appreciated!
Kind regards
Dustin
I would suggest you start thinking about adding DTOs for this purpose to also avoid exposing too much information to your clients for security but also network performance concerns. I think this is a perfect use case for Blaze-Persistence Entity Views.
I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.
A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:
#EntityView(WorldEntity.class)
public interface WorldEntityDto {
#IdMapping
Long getId();
String getName();
Set<EntityComponentDto> getComponents();
#EntityView(EntityComponent.class)
interface EntityComponentDto {
#IdMapping
Long getId();
String getName();
}
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
WorldEntityDto a = entityViewManager.find(entityManager, WorldEntityDto.class, id);
The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
Page<WorldEntityDto> findAll(Pageable pageable);
The best part is, it will only fetch the state that is actually necessary!
I was going through a document and I came across a term called DAO. I found out that it is a Data Access Object. Can someone please explain me what this actually is?
I know that it is some kind of an interface for accessing data from different types of sources, in the middle of this little research of mine I bumped into a concept called data source or data source object, and things got messed up in my mind.
I really want to know what a DAO is programmatically in terms of where it is used. How it is used? Any links to pages that explain this concept from the very basic stuff is also appreciated.
The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage.
That definition from:
http://en.wikipedia.org/wiki/Data_access_object
Check also the sequence diagram here:
http://www.oracle.com/technetwork/java/dataaccessobject-138824.html
Maybe a simple example can help you understand the concept:
Let's say we have an entity to represent an employee:
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The employee entities will be persisted into a corresponding Employee table in a database.
A simple DAO interface to handle the database operation required to manipulate an employee entity will be like:
interface EmployeeDAO {
List<Employee> findAll();
List<Employee> findById();
List<Employee> findByName();
boolean insertEmployee(Employee employee);
boolean updateEmployee(Employee employee);
boolean deleteEmployee(Employee employee);
}
Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc.
What is DATA ACCESS OBJECT (DAO) -
It is a object/interface, which is used to access data from database of data storage.
WHY WE USE DAO:
To abstract the retrieval of data from a data resource such as a database.
The concept is to "separate a data resource's client interface from its data access mechanism."
The problem with accessing data directly is that the source of the data can change. Consider, for example, that your application is deployed in an environment that accesses an Oracle database. Then it is subsequently deployed to an environment that uses Microsoft SQL Server. If your application uses stored procedures and database-specific code (such as generating a number sequence), how do you handle that in your application? You have two options:
Rewrite your application to use SQL Server instead of Oracle (or add conditional code to handle the differences), or
Create a layer in-between your application logic and data access layers
The DAO Pattern consists of the following:
Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).
Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource
which can be database / xml or any other storage mechanism.
Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.
See examples
I hope this has cleared up your understanding of DAO!
DAO (Data Access Object) is a very used design pattern in enterprise applications. It basically is the module that is used to access data from every source (DBMS, XML and so on). I suggest you to read some examples, like this one:
DAO Example
Please note that there are different ways to implements the original DAO Pattern, and there are many frameworks that can simplify your work. For example, the ORM (Object Relational Mapping) frameworks like iBatis or Hibernate, are used to map the result of SQL queries to java objects.
Hope it helps,
Bye!
Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern.
Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).
Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource which can be database / xml or any other storage mechanism.
Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.
Sample code here..
Don't get confused with too many explanations. DAO: From the name itself it means Accessing Data using Object. DAO is separated from other Business Logic.
I am going to be general and not specific to Java as DAO and ORM are used in all languages.
To understand DAO you first need to understand ORM (Object Relational Mapping). This means that if you have a table called "person" with columns "name" and "age", then you would create object-template for that table:
type Person {
name
age
}
Now with help of DAO instead of writing some specific queries, to fetch all persons, for what ever type of db you are using (which can be error-prone) instead you do:
list persons = DAO.getPersons();
...
person = DAO.getPersonWithName("John");
age = person.age;
You do not write the DAO abstraction yourself, instead it is usually part of some opensource project, depending on what language and framework you are using.
Now to the main question here. "..where it is used..". Well usually if you are writing complex business and domain specific code your life will be very difficult without DAO. Of course you do not need to use ORM and DAO provided, instead you can write your own abstraction and native queries. I have done that in the past and almost always regretted it later.
I think the best example (along with explanations) you can find on the oracle website : here. Another good tuturial could be found here.
Spring JPA DAO
For example we have some entity Group.
For this entity we create the repository GroupRepository.
public interface GroupRepository extends JpaRepository<Group, Long> {
}
Then we need to create a service layer with which we will use this repository.
public interface Service<T, ID> {
T save(T entity);
void deleteById(ID id);
List<T> findAll();
T getOne(ID id);
T editEntity(T entity);
Optional<T> findById(ID id);
}
public abstract class AbstractService<T, ID, R extends JpaRepository<T, ID>> implements Service<T, ID> {
private final R repository;
protected AbstractService(R repository) {
this.repository = repository;
}
#Override
public T save(T entity) {
return repository.save(entity);
}
#Override
public void deleteById(ID id) {
repository.deleteById(id);
}
#Override
public List<T> findAll() {
return repository.findAll();
}
#Override
public T getOne(ID id) {
return repository.getOne(id);
}
#Override
public Optional<T> findById(ID id) {
return repository.findById(id);
}
#Override
public T editEntity(T entity) {
return repository.saveAndFlush(entity);
}
}
#org.springframework.stereotype.Service
public class GroupServiceImpl extends AbstractService<Group, Long, GroupRepository> {
private final GroupRepository groupRepository;
#Autowired
protected GroupServiceImpl(GroupRepository repository) {
super(repository);
this.groupRepository = repository;
}
}
And in the controller we use this service.
#RestController
#RequestMapping("/api")
class GroupController {
private final Logger log = LoggerFactory.getLogger(GroupController.class);
private final GroupServiceImpl groupService;
#Autowired
public GroupController(GroupServiceImpl groupService) {
this.groupService = groupService;
}
#GetMapping("/groups")
Collection<Group> groups() {
return groupService.findAll();
}
#GetMapping("/group/{id}")
ResponseEntity<?> getGroup(#PathVariable Long id) {
Optional<Group> group = groupService.findById(id);
return group.map(response -> ResponseEntity.ok().body(response))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
#PostMapping("/group")
ResponseEntity<Group> createGroup(#Valid #RequestBody Group group) throws URISyntaxException {
log.info("Request to create group: {}", group);
Group result = groupService.save(group);
return ResponseEntity.created(new URI("/api/group/" + result.getId()))
.body(result);
}
#PutMapping("/group")
ResponseEntity<Group> updateGroup(#Valid #RequestBody Group group) {
log.info("Request to update group: {}", group);
Group result = groupService.save(group);
return ResponseEntity.ok().body(result);
}
#DeleteMapping("/group/{id}")
public ResponseEntity<?> deleteGroup(#PathVariable Long id) {
log.info("Request to delete group: {}", id);
groupService.deleteById(id);
return ResponseEntity.ok().build();
}
}
The Data Access Object manages the connection with the data source to obtain and store data.It abstracts the underlying data access implementation for the Business Object to enable transparent access to the data source.
A data source could be any database such as an RDBMS, XML repository or flat file system etc.
DAO is an act like as "Persistence Manager " in 3 tier architecture as well as DAO also design pattern as you can consult "Gang of Four" book.
Your application service layer just need to call the method of DAO class without knowing hidden & internal details of DAO's method.
Dao clases are used to reuse the jdbc logic & Dao(Data Access Object) is a design pattern.
dao is a simple java class which contains JDBC logic .
Data Access Layer has proven good in separate business logic layer and persistent layer. The DAO design pattern completely hides the data access implementation from its clients
The Java Data Access Object (Java DAO) is an important component in business applications. Business applications almost always need access to data from relational or object databases and the Java platform offers many techniques for accessingthis data. The oldest and most mature technique is to use the Java Database Connectivity (JDBC)API, which provides the capability to execute SQL queries against a databaseand then fetch the results, one column at a time.
Pojo also consider as Model class in Java where we can create getter and setter for particular variable defined in private .
Remember all variables are here declared with private modifier
I just want to explain it in my own way with a small story that I experienced in one of my projects. First I want to explain Why DAO is important? rather than go to What is DAO? for better understanding.
Why DAO is important?
In my one project of my project, I used Client.class which contains all the basic information of our system users. Where I need client then every time I need to do an ugly query where it is needed. Then I felt that decreases the readability and made a lot of redundant boilerplate code.
Then one of my senior developers introduced a QueryUtils.class where all queries are added using public static access modifier and then I don't need to do query everywhere. Suppose when I needed activated clients then I just call -
QueryUtils.findAllActivatedClients();
In this way, I made some optimizations of my code.
But there was another problem !!!
I felt that the QueryUtils.class was growing very highly. 100+ methods were included in that class which was also very cumbersome to read and use. Because this class contains other queries of another domain models ( For example- products, categories locations, etc ).
Then the superhero Mr. CTO introduced a new solution named DAO which solved the problem finally. I felt DAO is very domain-specific. For example, he created a DAO called ClientDAO.class where all Client.class related queries are found which seems very easy for me to use and maintain. The giant QueryUtils.class was broken down into many other domain-specific DAO for example - ProductsDAO.class, CategoriesDAO.class, etc which made the code more Readable, more Maintainable, more Decoupled.
What is DAO?
It is an object or interface, which made an easy way to access data from the database without writing complex and ugly queries every time in a reusable way.
I was going through a document and I came across a term called DAO. I found out that it is a Data Access Object. Can someone please explain me what this actually is?
I know that it is some kind of an interface for accessing data from different types of sources, in the middle of this little research of mine I bumped into a concept called data source or data source object, and things got messed up in my mind.
I really want to know what a DAO is programmatically in terms of where it is used. How it is used? Any links to pages that explain this concept from the very basic stuff is also appreciated.
The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage.
That definition from:
http://en.wikipedia.org/wiki/Data_access_object
Check also the sequence diagram here:
http://www.oracle.com/technetwork/java/dataaccessobject-138824.html
Maybe a simple example can help you understand the concept:
Let's say we have an entity to represent an employee:
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The employee entities will be persisted into a corresponding Employee table in a database.
A simple DAO interface to handle the database operation required to manipulate an employee entity will be like:
interface EmployeeDAO {
List<Employee> findAll();
List<Employee> findById();
List<Employee> findByName();
boolean insertEmployee(Employee employee);
boolean updateEmployee(Employee employee);
boolean deleteEmployee(Employee employee);
}
Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc.
What is DATA ACCESS OBJECT (DAO) -
It is a object/interface, which is used to access data from database of data storage.
WHY WE USE DAO:
To abstract the retrieval of data from a data resource such as a database.
The concept is to "separate a data resource's client interface from its data access mechanism."
The problem with accessing data directly is that the source of the data can change. Consider, for example, that your application is deployed in an environment that accesses an Oracle database. Then it is subsequently deployed to an environment that uses Microsoft SQL Server. If your application uses stored procedures and database-specific code (such as generating a number sequence), how do you handle that in your application? You have two options:
Rewrite your application to use SQL Server instead of Oracle (or add conditional code to handle the differences), or
Create a layer in-between your application logic and data access layers
The DAO Pattern consists of the following:
Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).
Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource
which can be database / xml or any other storage mechanism.
Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.
See examples
I hope this has cleared up your understanding of DAO!
DAO (Data Access Object) is a very used design pattern in enterprise applications. It basically is the module that is used to access data from every source (DBMS, XML and so on). I suggest you to read some examples, like this one:
DAO Example
Please note that there are different ways to implements the original DAO Pattern, and there are many frameworks that can simplify your work. For example, the ORM (Object Relational Mapping) frameworks like iBatis or Hibernate, are used to map the result of SQL queries to java objects.
Hope it helps,
Bye!
Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern.
Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).
Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource which can be database / xml or any other storage mechanism.
Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.
Sample code here..
Don't get confused with too many explanations. DAO: From the name itself it means Accessing Data using Object. DAO is separated from other Business Logic.
I am going to be general and not specific to Java as DAO and ORM are used in all languages.
To understand DAO you first need to understand ORM (Object Relational Mapping). This means that if you have a table called "person" with columns "name" and "age", then you would create object-template for that table:
type Person {
name
age
}
Now with help of DAO instead of writing some specific queries, to fetch all persons, for what ever type of db you are using (which can be error-prone) instead you do:
list persons = DAO.getPersons();
...
person = DAO.getPersonWithName("John");
age = person.age;
You do not write the DAO abstraction yourself, instead it is usually part of some opensource project, depending on what language and framework you are using.
Now to the main question here. "..where it is used..". Well usually if you are writing complex business and domain specific code your life will be very difficult without DAO. Of course you do not need to use ORM and DAO provided, instead you can write your own abstraction and native queries. I have done that in the past and almost always regretted it later.
I think the best example (along with explanations) you can find on the oracle website : here. Another good tuturial could be found here.
Spring JPA DAO
For example we have some entity Group.
For this entity we create the repository GroupRepository.
public interface GroupRepository extends JpaRepository<Group, Long> {
}
Then we need to create a service layer with which we will use this repository.
public interface Service<T, ID> {
T save(T entity);
void deleteById(ID id);
List<T> findAll();
T getOne(ID id);
T editEntity(T entity);
Optional<T> findById(ID id);
}
public abstract class AbstractService<T, ID, R extends JpaRepository<T, ID>> implements Service<T, ID> {
private final R repository;
protected AbstractService(R repository) {
this.repository = repository;
}
#Override
public T save(T entity) {
return repository.save(entity);
}
#Override
public void deleteById(ID id) {
repository.deleteById(id);
}
#Override
public List<T> findAll() {
return repository.findAll();
}
#Override
public T getOne(ID id) {
return repository.getOne(id);
}
#Override
public Optional<T> findById(ID id) {
return repository.findById(id);
}
#Override
public T editEntity(T entity) {
return repository.saveAndFlush(entity);
}
}
#org.springframework.stereotype.Service
public class GroupServiceImpl extends AbstractService<Group, Long, GroupRepository> {
private final GroupRepository groupRepository;
#Autowired
protected GroupServiceImpl(GroupRepository repository) {
super(repository);
this.groupRepository = repository;
}
}
And in the controller we use this service.
#RestController
#RequestMapping("/api")
class GroupController {
private final Logger log = LoggerFactory.getLogger(GroupController.class);
private final GroupServiceImpl groupService;
#Autowired
public GroupController(GroupServiceImpl groupService) {
this.groupService = groupService;
}
#GetMapping("/groups")
Collection<Group> groups() {
return groupService.findAll();
}
#GetMapping("/group/{id}")
ResponseEntity<?> getGroup(#PathVariable Long id) {
Optional<Group> group = groupService.findById(id);
return group.map(response -> ResponseEntity.ok().body(response))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
#PostMapping("/group")
ResponseEntity<Group> createGroup(#Valid #RequestBody Group group) throws URISyntaxException {
log.info("Request to create group: {}", group);
Group result = groupService.save(group);
return ResponseEntity.created(new URI("/api/group/" + result.getId()))
.body(result);
}
#PutMapping("/group")
ResponseEntity<Group> updateGroup(#Valid #RequestBody Group group) {
log.info("Request to update group: {}", group);
Group result = groupService.save(group);
return ResponseEntity.ok().body(result);
}
#DeleteMapping("/group/{id}")
public ResponseEntity<?> deleteGroup(#PathVariable Long id) {
log.info("Request to delete group: {}", id);
groupService.deleteById(id);
return ResponseEntity.ok().build();
}
}
The Data Access Object manages the connection with the data source to obtain and store data.It abstracts the underlying data access implementation for the Business Object to enable transparent access to the data source.
A data source could be any database such as an RDBMS, XML repository or flat file system etc.
DAO is an act like as "Persistence Manager " in 3 tier architecture as well as DAO also design pattern as you can consult "Gang of Four" book.
Your application service layer just need to call the method of DAO class without knowing hidden & internal details of DAO's method.
Dao clases are used to reuse the jdbc logic & Dao(Data Access Object) is a design pattern.
dao is a simple java class which contains JDBC logic .
Data Access Layer has proven good in separate business logic layer and persistent layer. The DAO design pattern completely hides the data access implementation from its clients
The Java Data Access Object (Java DAO) is an important component in business applications. Business applications almost always need access to data from relational or object databases and the Java platform offers many techniques for accessingthis data. The oldest and most mature technique is to use the Java Database Connectivity (JDBC)API, which provides the capability to execute SQL queries against a databaseand then fetch the results, one column at a time.
Pojo also consider as Model class in Java where we can create getter and setter for particular variable defined in private .
Remember all variables are here declared with private modifier
I just want to explain it in my own way with a small story that I experienced in one of my projects. First I want to explain Why DAO is important? rather than go to What is DAO? for better understanding.
Why DAO is important?
In my one project of my project, I used Client.class which contains all the basic information of our system users. Where I need client then every time I need to do an ugly query where it is needed. Then I felt that decreases the readability and made a lot of redundant boilerplate code.
Then one of my senior developers introduced a QueryUtils.class where all queries are added using public static access modifier and then I don't need to do query everywhere. Suppose when I needed activated clients then I just call -
QueryUtils.findAllActivatedClients();
In this way, I made some optimizations of my code.
But there was another problem !!!
I felt that the QueryUtils.class was growing very highly. 100+ methods were included in that class which was also very cumbersome to read and use. Because this class contains other queries of another domain models ( For example- products, categories locations, etc ).
Then the superhero Mr. CTO introduced a new solution named DAO which solved the problem finally. I felt DAO is very domain-specific. For example, he created a DAO called ClientDAO.class where all Client.class related queries are found which seems very easy for me to use and maintain. The giant QueryUtils.class was broken down into many other domain-specific DAO for example - ProductsDAO.class, CategoriesDAO.class, etc which made the code more Readable, more Maintainable, more Decoupled.
What is DAO?
It is an object or interface, which made an easy way to access data from the database without writing complex and ugly queries every time in a reusable way.
I am developing a simple forum web application using SpringMVC, JPA2.
I have created JPA entities like User, Forum, Post etc which reflects the DB table structure.
But while displaying the data on UI I need DTOs as I can't always hold the data to be displayed on UI using Entities.
For ex: Change Password screen. Here I need to hold Old Pwd, New Password and Confirm New Pwd. But User entity won't have Old/New/Confirm Pwd fields, it just has Password. So I need to create DTOs which are just data carriers between Web and service layers.
My question is while creating DTO objects, should I put all the properties in DTO itself or wrap the Entity in DTO and add additional properties needed?
Ex: For Edit User Screen,
public class UserDTO
{
private User user; // User is a JPA entity
// setters & getters
}
With this I can pass the underlying User entity to my service layer. But while binding UI properties to DTO I need to associate PropertyEditors.
(or)
public class UserDTO
{
private String userId;
private String userName;
private String password;
// setters & getters
}
With this approach, I need to convert & copy the DTO properties into JPA entities and pass to Service layer.
Which approach is better? Or is there any other approach without DTOs altogether?
Your first approach still carries the Entity object itself to the presentation layer. If you have additional parameters that are not coming from the database, and your persistence context is still available, then this approach is sufficient.
The second approach requires duplication of code, which is not ideal.
If the persistence context is not available, I would suggest detaching the Entity from the persistence context using EntityManager.detach(), rather than creating a parallel hierarchy of beans.
On the other hand, if data is coming in from the presentation layer, you will need to load the Entity from the database (using find() or something similar) and update it, or merge() it into the persistence context.