{"id":61270,"date":"2024-04-18T11:39:43","date_gmt":"2024-04-18T06:09:43","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=61270"},"modified":"2024-04-18T11:39:43","modified_gmt":"2024-04-18T06:09:43","slug":"understanding-data-projection-in-spring-boot-with-hibernate","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/understanding-data-projection-in-spring-boot-with-hibernate\/","title":{"rendered":"Understanding Data Projection in Spring Boot with Hibernate"},"content":{"rendered":"<p style=\"text-align: left;\">This article will delve into data projection in the context of Spring Boot and Hibernate. This blog will examine the significance of data projection, its advantages, and disadvantages. Furthermore, I will provide a comprehensive explanation of how to implement it in your Spring Boot applications efficiently.<\/p>\n<h2><strong class=\"lp fs\">What is data Projection?<\/strong><\/h2>\n<p>Data projection in database querying involves selecting specific attributes from one or multiple entities or tables and retrieving only those attributes in the result set. This technique is useful when you do not require the complete entity object and aims to enhance performance by fetching only the necessary data.<\/p>\n<h2><strong class=\"lp fs\">Why use data projection?<\/strong><\/h2>\n<p id=\"60ca\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\" data-selectable-paragraph=\"\">Using data projection in Spring Boot offers several benefits:<\/p>\n<ol>\n<li id=\"8635\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Performance Optimization:<\/strong>\u00a0Data projection enables the retrieval of specific fields from database queries, rather than fetching complete objects. This leads to a notable decrease in data transfer between the application and the database, ultimately improving response times and enhancing overall performance.<\/li>\n<li id=\"7307\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Bandwidth Efficiency:<\/strong>\u00a0Opting for essential fields helps reduce the network bandwidth usage of your application, which is crucial when dealing with extensive datasets. This is especially significant for applications operating in settings with restricted network resources or when data transfer expenses need to be considered.<\/li>\n<li id=\"3307\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Reduced Memory Usage:<\/strong>\u00a0By fetching only the necessary fields from the database, it reduces the amount of memory required to store the data. This optimization can significantly enhance memory usage efficiency, particularly when dealing with extensive datasets.<\/li>\n<li id=\"2c8d\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Improved Security:<\/strong>\u00a0Data projection can enhance security by restricting the disclosure of sensitive or redundant data fields in the responses of your application.<\/li>\n<\/ol>\n<h2><strong class=\"lp fs\">Data Projection Techniques :<\/strong><\/h2>\n<ul>\n<li><strong class=\"lp fs\">JPQL (Java Persistence Query Language) Queries<\/strong>: JPQL allows developers to write SQL-like queries to retrieve specific fields from entities. By selecting only the required fields in the SELECT clause, you can achieve data projection.<\/li>\n<\/ul>\n<pre>     <span class=\"hljs-meta\">@Query(\"SELECT NEW com.ttn.dataProjection.dto.UserDetailsProjectionDTO(u.name, u.email, a.city, a.pincode) FROM UserDetails u JOIN u.address a\")<\/span>\r\n     List&lt;UserDetailsProjectionDTO&gt; <span class=\"hljs-title.function\">findAllByActiveTrueOrderByNameDesc<\/span><span class=\"hljs-params\">()<\/span>;<\/pre>\n<ul>\n<li><strong class=\"lp fs\">Spring Data JPA Projections:<\/strong>\u00a0Spring Data JPA provides a powerful feature called projections, which allows defining interfaces to represent the desired subset of data. These projections can be used directly in repository methods.<\/li>\n<\/ul>\n<pre><span class=\"hljs-meta\">@Data<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">UserDetailsProjectionDTO<\/span> {\r\n<span class=\"hljs-keyword\">private<\/span> String name;\r\n<span class=\"hljs-keyword\">private<\/span> String email;\r\n<span class=\"hljs-keyword\">private<\/span> String city;\r\n<span class=\"hljs-keyword\">private<\/span> String pincode;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.function\">UserDetailsProjectionDTO<\/span><span class=\"hljs-params\">(String name, String email, String city, String pincode)<\/span> {\r\n<span class=\"hljs-built_in\">this<\/span>.name = name;\r\n<span class=\"hljs-built_in\">this<\/span>.email = email;\r\n<span class=\"hljs-built_in\">this<\/span>.city = city;\r\n<span class=\"hljs-built_in\">this<\/span>.pincode = pincode;\r\n}\r\n}\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title.class\">RolesProjection<\/span> {\r\n\r\nString <span class=\"hljs-title.function\">getName<\/span><span class=\"hljs-params\">()<\/span>;\r\n}\r\n\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title.class\">AddressProjection<\/span> {\r\nString <span class=\"hljs-title.function\">getCity<\/span><span class=\"hljs-params\">()<\/span>;\r\nString <span class=\"hljs-title.function\">getPincode<\/span><span class=\"hljs-params\">()<\/span>;\r\n}<\/pre>\n<ul>\n<li><strong class=\"lp fs\">DTO (Data Transfer Object) Projection:<\/strong> Another common approach is to use DTOs to represent the projected data. DTOs are plain Java objects that contain only the fields needed for a particular use case.<\/li>\n<\/ul>\n<pre><span class=\"hljs-comment\">\/\/create a class with name UserDetailsDto,AddressDto and RolesDto and add below code.<\/span>\r\n<span class=\"hljs-meta\">@Data<\/span>\r\n<span class=\"hljs-meta\">@JsonPropertyOrder({\"name\",\"email\",\"mobile\",\"address\",\"roles\"})<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">UserDetailsDto<\/span> {\r\n<span class=\"hljs-keyword\">private<\/span> String name;\r\n<span class=\"hljs-keyword\">private<\/span> String email;\r\n<span class=\"hljs-keyword\">private<\/span> String mobile;\r\n<span class=\"hljs-keyword\">private<\/span> List&lt;AddressDto&gt; address;\r\n<span class=\"hljs-keyword\">private<\/span> List&lt;RolesDto&gt; roles;\r\n}\r\n\r\n<span class=\"hljs-meta\">@Data<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">AddressDto<\/span> {\r\n<span class=\"hljs-keyword\">private<\/span> String city;\r\n<span class=\"hljs-keyword\">private<\/span> String pincode;\r\n}\r\n\r\n<span class=\"hljs-meta\">@Data<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">RolesDto<\/span> {\r\n<span class=\"hljs-keyword\">private<\/span> String name;\r\n}\r\n\r\n <span class=\"hljs-keyword\">public<\/span> List&lt;UserDetailsDto&gt; <span class=\"hljs-title.function\">getAllByDto<\/span><span class=\"hljs-params\">()<\/span> {\r\nList&lt;UserDetails&gt; userDetailsList= userDetailsRepository.findAll();\r\nList&lt;UserDetailsDto&gt; userDetailsDtoList=<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ArrayList<\/span>&lt;&gt;();\r\nuserDetailsList.forEach(userDetails -&gt; {\r\nUserDetailsDto userDetailsDto=<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">UserDetailsDto<\/span>();\r\nBeanUtils.copyProperties(userDetails,userDetailsDto);\r\nList&lt;AddressDto&gt; addressDtoList=<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ArrayList<\/span>&lt;&gt;();\r\nuserDetails.getAddress().forEach(address -&gt; {\r\nAddressDto addressDto=<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">AddressDto<\/span>();\r\nBeanUtils.copyProperties(address,addressDto);\r\naddressDtoList.add(addressDto);\r\n});\r\nuserDetailsDto.setAddress(addressDtoList);\r\nList&lt;RolesDto&gt; rolesDtoList=<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ArrayList<\/span>&lt;&gt;();\r\nuserDetails.getRoles().forEach(roles -&gt; {\r\nRolesDto rolesDto=<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">RolesDto<\/span>();\r\nBeanUtils.copyProperties(roles,rolesDto);\r\nrolesDtoList.add(rolesDto);\r\n});\r\nuserDetailsDto.setRoles(rolesDtoList);\r\nuserDetailsDtoList.add(userDetailsDto);\r\n});\r\n<span class=\"hljs-keyword\">return<\/span> userDetailsDtoList;\r\n}<\/pre>\n<h2><strong class=\"lp fs\">Project Details :<\/strong><\/h2>\n<p>In the context of a User Management System, let\u2019s consider three entities: UserDetails, Roles, and Address. Within this project, I have developed a set of five APIs to facilitate various functionalities.<\/p>\n<ol class=\"\">\n<li id=\"e86d\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\">Create API for the user.<\/li>\n<li id=\"974d\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\">Fetching All Users with Entity.<\/li>\n<li id=\"68db\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\">Retrieving user details, including specific fields of an entity, can be accomplished by utilizing the\u00a0<strong class=\"lp fs\">\u201cSpring Data JPA Projections\u201d<\/strong>\u00a0technique.<\/li>\n<li id=\"7934\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\">Retrieving user details, including specific fields of an entity, can be accomplished by utilizing the\u00a0<strong class=\"lp fs\">\u201cDTO (Data Transfer Object) Projection\u201d<\/strong>\u00a0technique.<\/li>\n<li id=\"f429\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\">Retrieving user details, including specific fields of an entity, can be accomplished by utilizing the\u00a0<strong class=\"lp fs\">\u201cJPQL (Java Persistence Query Language) Queries\u201d<\/strong>\u00a0technique.<\/li>\n<\/ol>\n<pre><span class=\"hljs-meta\">@RequestMapping(value = \"\/user\")<\/span>\r\n<span class=\"hljs-meta\">@RestController<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">UserController<\/span> {\r\n\r\n<span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> UserService userService;\r\n\r\n<span class=\"hljs-meta\">@Autowired<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.function\">UserController<\/span><span class=\"hljs-params\">(UserService userService)<\/span> {\r\n<span class=\"hljs-built_in\">this<\/span>.userService = userService;\r\n}\r\n\r\n<span class=\"hljs-meta\">@PostMapping(value = \"\/create\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> UserDetails <span class=\"hljs-title.function\">createUser<\/span><span class=\"hljs-params\">(<span class=\"hljs-meta\">@RequestBody<\/span> UserDetails userDetails)<\/span> {\r\n<span class=\"hljs-keyword\">return<\/span> userService.createUser(userDetails);\r\n}\r\n\r\n<span class=\"hljs-meta\">@GetMapping(value = \"\/getAll\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> List&lt;UserDetails&gt; <span class=\"hljs-title.function\">getAllUsers<\/span><span class=\"hljs-params\">()<\/span>{\r\n<span class=\"hljs-keyword\">return<\/span> userService.getAllUsers();\r\n}\r\n\r\n<span class=\"hljs-meta\">@GetMapping(value = \"\/getAllByProjection\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> List&lt;UserDetailsProjection&gt; <span class=\"hljs-title.function\">getAllByProjection<\/span><span class=\"hljs-params\">()<\/span>{\r\n<span class=\"hljs-keyword\">return<\/span> userService.getAllUsersByProjection();\r\n}\r\n<span class=\"hljs-meta\">@GetMapping(value = \"\/getAllByDto\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> List&lt;UserDetailsDto&gt; <span class=\"hljs-title.function\">getAllByDto<\/span><span class=\"hljs-params\">()<\/span>{\r\n<span class=\"hljs-keyword\">return<\/span> userService.getAllByDto();\r\n}\r\n\r\n<span class=\"hljs-meta\">@GetMapping(value = \"\/getAllByJPQL\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> List&lt;UserDetailsProjectionDTO&gt; <span class=\"hljs-title.function\">getAllByJPQL<\/span><span class=\"hljs-params\">()<\/span>{\r\n<span class=\"hljs-keyword\">return<\/span> userService.getAllByJPQL();\r\n}\r\n}<\/pre>\n<p>Below are some PROS and CONS of spring data projection techniques through which you can find out which technique you can use according to your situation.<\/p>\n<h3 id=\"f595\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong class=\"lp fs\">JPQL Queries:<\/strong><br \/>\nPros:<\/h3>\n<ol class=\"\">\n<li id=\"b684\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Fine-grained Control:<\/strong>\u00a0JPQL queries offer precise control over the fields to be selected, allowing developers to fetch exactly what they need.<\/li>\n<li id=\"0d7f\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Flexibility:<\/strong>\u00a0Developers can leverage the full power of JPQL, including joins, aggregates, and complex filtering conditions.<\/li>\n<li id=\"6f61\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Performance Optimization:<\/strong>\u00a0By selecting only necessary fields, JPQL queries can optimize database performance by reducing the amount of data transferred.<\/li>\n<\/ol>\n<h3 id=\"2378\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong>Cons :<\/strong><\/h3>\n<ol class=\"\">\n<li id=\"9fa2\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Syntax Complexity:<\/strong>\u00a0Writing JPQL queries can be more complex and verbose compared to other projection techniques, especially for developers less familiar with SQL-like syntax.<\/li>\n<li id=\"fcb1\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Potential for Typos and Errors:\u00a0<\/strong>Due to the dynamic nature of JPQL queries, there\u2019s a higher likelihood of typos or errors in query construction, leading to runtime issues.<\/li>\n<\/ol>\n<h2 id=\"2cd4\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong class=\"lp fs\">Spring Data JPA Projections:<\/strong><\/h2>\n<h3 id=\"07ec\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong>Pros :<\/strong><\/h3>\n<ol class=\"\">\n<li id=\"e514\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Type Safety:\u00a0<\/strong>Spring Data JPA projections provide type-safe interfaces or classes for representing projected data, reducing the risk of runtime errors.<\/li>\n<li id=\"32bb\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Simplicity:\u00a0<\/strong>Projections can be defined as interfaces or classes with getter methods, making them easy to understand and maintain.<\/li>\n<li id=\"fdd8\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Integrated with Repository:<\/strong>\u00a0Projections can be seamlessly integrated into repository methods, allowing for clean and concise query definitions.<\/li>\n<li id=\"f52c\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Reduced Boilerplate Code:<\/strong>\u00a0Projections eliminate the need for manually mapping query results to DTOs or other data structures, reducing boilerplate code.<\/li>\n<\/ol>\n<h3 id=\"5883\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong>Cons:<\/strong><\/h3>\n<ol class=\"\">\n<li id=\"dd44\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Limited Expressiveness:\u00a0<\/strong>When it comes to complex query constructs, JPQL queries have broader support in comparison to projections, which could limit their usefulness in certain situations.<\/li>\n<li id=\"582d\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Static Definition:<\/strong>\u00a0To create projections, it is necessary to have pre-defined interfaces. However, this approach might not be very flexible for dynamic projections.<\/li>\n<\/ol>\n<h2 id=\"570b\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong class=\"lp fs\">DTO Projection:<\/strong><\/h2>\n<h3 id=\"7286\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong>Pros:<\/strong><\/h3>\n<ol class=\"\">\n<li id=\"21b3\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Flexibility:<\/strong>\u00a0DTO projection allows developers to define custom data transfer objects tailored to specific use cases, providing maximum flexibility.<\/li>\n<li id=\"4bc6\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Serialization Control:\u00a0<\/strong>DTOs enable fine-grained control over the data to be serialized and transmitted over the network, improving performance and reducing bandwidth usage.<\/li>\n<li id=\"8431\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Encapsulation:<\/strong>\u00a0DTOs encapsulate projected data, promoting better encapsulation and separation of concerns within the application architecture.<\/li>\n<li id=\"1c01\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Reusability:\u00a0<\/strong>DTOs can be reused across multiple queries or use cases, promoting code reuse and maintainability.<\/li>\n<\/ol>\n<h3 id=\"59ca\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong>Cons:<\/strong><\/h3>\n<ol class=\"\">\n<li id=\"f0c1\" class=\"ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Additional Mapping Overhead:<\/strong>\u00a0DTO projection requires additional mapping code to transform entity objects into DTOs, potentially introducing overhead, especially for complex mappings.<\/li>\n<li id=\"d10b\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Manual Synchronization:<\/strong>\u00a0Developers must ensure synchronization between entity attributes and DTO fields, which can be cumbersome and error-prone, especially in large projects.<\/li>\n<li id=\"e999\" class=\"ln lo fr lp b lq mo ls lt lu mp lw lx ly mq ma mb mc mr me mf mg ms mi mj mk ml mm mn bj\" data-selectable-paragraph=\"\"><strong class=\"lp fs\">Boilerplate Code:\u00a0<\/strong>DTOs often require writing additional boilerplate code for defining classes, constructors, getters, and setters, increasing development effort and verbosity.<\/li>\n<\/ol>\n<h2 id=\"3ff5\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\"><strong>Project Souce Code :<\/strong><\/h2>\n<p id=\"57ae\" class=\"pw-post-body-paragraph ln lo fr lp b lq lr ls lt lu lv lw lx ly lz ma mb mc md me mf mg mh mi mj mk fk bj\" data-selectable-paragraph=\"\">https:\/\/github.com\/ankit25496kumar\/data-projection<\/p>\n<h2>Reference :<\/h2>\n<p>https:\/\/medium.com\/@ankit25496kumar\/understanding-data-projection-in-spring-boot-with-hibernate-23a581a13781<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>This article will delve into data projection in the context of Spring Boot and Hibernate. This blog will examine the significance of data projection, its advantages, and disadvantages. Furthermore, I will provide a comprehensive explanation of how to implement it in your Spring Boot applications efficiently. What is data Projection? Data projection in database querying [&hellip;]<\/p>\n","protected":false},"author":1765,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":191},"categories":[446,1994],"tags":[5831,5830,2072],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61270"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1765"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=61270"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61270\/revisions"}],"predecessor-version":[{"id":61359,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61270\/revisions\/61359"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=61270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=61270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=61270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}