Including Only Certain Columns from Child in Parent Entity

Hi,

New to Hibernate and Java. I’ve successfully created several entities which I plan to use to perform CRUD operations against the underlying tables. I’ve also created an entity which joins data (using one-to-one unidirectional mapping) from the child entity (Department) to the parent (Employee).

The Department entity contains columns not required in the context of the Employee entity. In the Employee entity I only want to show the Department ID and Name, whereas in the Department entity I want to show all columns. Further, in the context of the Employee entity the Department ID should be insertable/updateable, but the Department Name should be included for reference only.

How would I go about achieving this?

Thanks in advance for any assistance.

Use a DTO model for your projection purposes with e.g. 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(Employee.class)
public interface EmployeeDto {
    @IdMapping
    Long getId();
    String getName();
    DepartmentSimple getDepartment();

    @EntityView(Department.class)
    interface DepartmentSimple {
        @IdMapping
        Long getId();
        String getName();
    }
}

@EntityView(Department.class)
public interface DepartmentDto {
    @IdMapping
    Long getId();
    String getName();
    Set<EmployeeSimple> getEmployees();

    @EntityView(Employee.class)
    interface EmployeeSimple {
        @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.

UserDto a = entityViewManager.find(entityManager, UserDto.class, id);

The Spring Data integration allows you to use it almost like Spring Data Projections: Blaze Persistence - Entity View Module

Page<EmployeeDto> findAll(Pageable pageable);

The best part is, it will only fetch the state that is actually necessary!

Hi beikov,

Thanks for the response. So far, everything I’ve read about DTO’s says they are a projection for read-only operations. As mentioned in my initial post I would need the ability to modify the ID of Department in the context of Employee (with the value of Name updating based on ID).

Additionally, in terms of scaling I’m not sure that passing a subset of columns from a full-column resultset would be the most efficient use of resources (though I’m not 100% certain that I’m understanding the concept correctly). Instead I’d like to only pull/use the columns that are required from the child object’s base table for use in the parent entity.

Thanks!

So far, everything I’ve read about DTO’s says they are a projection for read-only operations. As mentioned in my initial post I would need the ability to modify the ID of Department in the context of Employee (with the value of Name updating based on ID).

You can always also run UPDATE statements directly or select an entity, modify it and flush to submit the changes to the database. Or you use updatable entity views with Blaze-Persistence which does all of this for you.

Additionally, in terms of scaling I’m not sure that passing a subset of columns from a full-column resultset would be the most efficient use of resources (though I’m not 100% certain that I’m understanding the concept correctly). Instead I’d like to only pull/use the columns that are required from the child object’s base table for use in the parent entity.

That’s the nice thing about Blaze-Persistence Entity-Views. By using it, you will only select columns that are actually needed without the overhead of writing a separate select statement and wiring up the tuple values to a constructor.
With technologies like Spring Data Projections or MapStruct you usually select the whole entities with all the data but just pass a subset of data to the DTO constructors. That is not very efficient as it unnecessarily selects data from the database.

Hi,

Going on your suggestion I have been looking into Blaze Persistence, but I’m having trouble getting things started. I have added the maven dependencies to my Spring Boot application and am now trying to add the CriteriaBuilderFactory per the Spring instructions (section 1.2.4) of the documentation (Blaze Persistence - Criteria API for JPA backends).

The documentation isn’t clear as to where that code snippet is supposed to go, or what libraries to import in the class in order to make it work. Further, should this code go within my main startup class, or a separate class? I do not possess a deep understanding of Java so I apologize if my questions seem simple.

Thanks!

You can take a look at the quickstarts to play around with it and port that into your application.