0

I have a few entities, these are hibernate mapped. Menu, MenuDetails, Languages. When a menu excist the most important property of that entity is its id. There are a few languages with id 'nl', 'en', fr'. For example when users use 3 languages they can create a menu with 3 MenuDetails in a OneToMany List inside menu. When I try to fetch the menu by id, I get all thee of the MenuDetails. But how am I able to Fetch a Menu with only one MenuDetails entity in the List with forexample id 'nl'? Can someone help me. Now I do the associations by hand, but that takes to much network and cpu recourses.

@Entity
@Table(name = "menus")
public class Menu {
    @Id
    @Column(length = 36, insertable = false, updatable = false, nullable = false)
    private String id = UUID.randomUUID().toString();

    private String shopId;

    private Integer sequence;

    private Character status;

    @Transient
    private List<MenuDetails> menuDetails;

    @Transient
    private List<Category> categories;

    public Menu setId(String id) {
        this.id = id;
        return this;
    }

    public String getId() {
        return id;
    }

    public Menu setShopId(String shopId) {
        this.shopId = shopId;
        return this;
    }

    public String getShopId() {
        return shopId;
    }

    public Menu setMenuDetails(List<MenuDetails> menuDetails) {
        this.menuDetails = menuDetails;
        return this;
    }

    public List<MenuDetails> getMenuDetails() {
        return menuDetails;
    }

    public Menu setCategories(List<Category> categories) {
        this.categories = categories;
        return this;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public Integer getSequence() {
        return sequence;
    }

    public Menu setSequence(Integer sequence) {
        this.sequence = sequence;
        return this;
    }

    public Character getStatus() {
        return status;
    }

    public Menu setStatus(Character status) {
        this.status = status;
        return this;
    }
}
@Entity
@IdClass(MenuDetailsId.class)
@Table(name = "menu_details")
public class MenuDetails {
    @Id
    private String menuId;

    @Id
    private String languageCode;

    private String name;

    // Empty constructor required for composite primary key
    public MenuDetails(){ super(); }

    public String getMenuId() { return menuId; }
    public MenuDetails setMenuId(String menuId) {
        this.menuId = menuId;
        return this;
    }

    public String getLanguageCode() { return languageCode; }
    public MenuDetails setLanguageCode(String languageCode) {
        this.languageCode = languageCode;
        return this;
    }

    public String getName() { return name; }
    public MenuDetails setName(String name) {
        this.name = name;
        return this;
    }

    public static MenuDetails createEmpty(String menuId, String languageCode) {
        return new MenuDetails()
                .setMenuId(menuId)
                .setLanguageCode(languageCode)
                .setName("");
    }
}
@Entity
@Table(name = "languages")
public class Language {
    @Id
    @Column(length = 36, updatable = false, nullable = false)
    private String id;

    @Column(length = 5)
    private String code;
    private String name;
    private String iconUrl;

    public Language setId(String id){
        this.id = id;
        return this;
    }
    public String getId(){ return id; }

    public Language setCode(String code){
        this.code = code;
        return this;
    }
    public String getCode(){ return code; }

    public Language setName(String name){
        this.name = name;
        return this;
    }
    public String getName(){ return name; }

    public Language setIconUrl(String iconUrl){
        this.iconUrl = iconUrl;
        return this;
    }
    public String getIconUrl(){ return iconUrl; }
}
5
  • Can you share your current approach and entities in question?
    – Thibstars
    Oct 5, 2020 at 15:04
  • code @Entity @Table(name = "menus") public class Menu { @Id @Column(length = 36, insertable = false, updatable = false, nullable = false) private String id = UUID.randomUUID().toString(); private String shopId; private Integer sequence; private Character status; @Transient private List<MenuDetails> menuDetails; @Transient private List<Category> categories; code Oct 5, 2020 at 15:18
  • code @Entity @IdClass(MenuDetailsId.class) @Table(name = "menu_details") public class MenuDetails { @Id private String menuId; @Id private String languageCode; private String name; Oct 5, 2020 at 15:19
  • code @Entity @Table(name = "languages") public class Language { @Id @Column(length = 36, updatable = false, nullable = false) private String id; @Column(length = 5) private String code; private String name; private String iconUrl; Oct 5, 2020 at 15:19
  • There are also entities like, categories, category_details, products, product_details, supplements, condiments, allergens, zones, tables, ... Oct 5, 2020 at 15:25

1 Answer 1

0

If you use join fetching, you are out of luck. JPA demands that the state of managed entities and the database must be in sync at the end of a transaction, so fetching only a partial collection of an entity would essentially delete the non-fetched elements, which is why JPA does not allow this. You can however do it with Hibernate but I would not recommend it because it could lead to deletions.

So to get what you want, you need to write a HQL or JPA Criteria query and fetch data into DTOs. In the query, you select all fields you need and can define the join condition you mentioned. 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(Menu.class)
public interface MenuDto {
    @IdMapping
    String getId();
    String getShopId();
    Integer getSequence();
    Character getCharacter();
    @Mapping("menuDetails[languageCode = 'nl']") // Could also use a parameter e.g. :language
    MenuDetailsDto getMenu();

    @EntityView(MenuDetails.class)
    interface MenuDetailsDto {
        @IdMapping
        String getId();
        String getName();
    }

    // Other mappings
}

Querying is a matter of applying the entity view to a query, the simplest being just a query by id.

MenuDto a = entityViewManager.find(entityManager, MenuDto.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

The only "catch" is, that id-classes are not fully supported, but you can use embeddable classes for composite ids instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.