Hibernate dao design question

I want to create a generic DAO to handle CRUD in my hibernate application. My entities have most associations as LASIA. But I find that hibernation is as efficient as possible with SELECTs, which I need to create several methods for my DAOs. Here is what I mean:

Entity A has two associations. Sometimes I want to get this object without loading associations, and sometimes I want it to be completely populated, so I put two methods in the DAO:

getWhatever()
getWhateverWithLoadedAssociations()

and I would have two different queries: one with no fetch link, and the other with fetch. As a result, sleep mode always makes one choice, whether it is LAZI or not, because I know that I want to get ahead.

The problem with this is keeping SELECT or two, difficulty with adding due to the number of methods.

Is this for extreme? Should I just getWhatever () and just let hibernate make another choice when I need data for an association, although I could save by not doing this SELECT?

Hope this is not too confusing. I am trying to figure out the cost of SELECTS due to lazy loading as well as code complexity.

thank

+3
source share
6 answers

IMHO, I will add a few of the following methods to the general DAO:

findById(Id id);//lazy load
loadTree(Id id);//load full graph of the entity
load(Id id, String... includePaths);// just load some associations of the entity
+1
source

, , ? , , .

HQL, , , :

getWhatever (WhateverFetchMode f)

f , HQL , .

public List<MyObject> getList(boolean fetchProp1) {
         return em.createQuery("select r" +
                "from Object as r " +
                fetchProp1 ? "left join fetch r.prop1 " : "")
                .getResultList();
}

public List<MyObject> getList(WhateverFetchMode fetchProp1) {
         return em.createQuery("select r" +
                "from Object as r " +
                fetchProp1.toHql()) // This returns the right join fetch
                .getResultList();
}
+2

, . , , .

Hibernate , , , .

+2

? getWhatever() hibernate , , , SELECT?

/ , , , , getWhatever() getWhateverWithLoadedAssociations()? SQL-.

, getWhatever():

  • - SQL-
  • - SQL- .

.

, - , DAO, , ; , .

+2

dao :

public void initialize(Object entity);

Hibernate.initialize(entity);

0

-, , .

, , .

, , , .

0

Source: https://habr.com/ru/post/1757613/


All Articles