开发者

The easiest way to created on-the-fly structs to work with hibernate

I'm using hibernate and i need to fetch the db for some data. I have a table with many fields, and i want in different places to fetch different fields. I can use AliasToBeanResultTransformer and create my own class that would represent only the fields that I want to fetch. But i have many usages like this. I dont want to find myself creating many classes with 2 members just t开发者_如何学Co get 2 fields from the db.

in idea?

thanks.


I don't really see any other way. The other options are crappy:

  • use a class containing all the possible fields, and only populate the ones you fetched (the other ones staying at null): how will the calling code know which fields are populated and which are not? This will become a maintenance nightmare
  • use a Map to store everything: even worse: you don't know what might be in the map, and the code will be full of casts.

If you have 5 use-cases having each a different view of what a Foo contains, you'll have to create 5 different Foo DTOs.

Is it really necessary to fetch only some of the fields? Couldn't you use the same entity class for all the use-cases? Is it a performance problem? If so, have you measured a performance problem? Most of the time, a query takes time to execute because it takes time finding the data to return, but not because of the amount of data it returns. Make sure that you don't optimize prematurely.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜