why hibernate forcing serialization in session.get method

前端 未结 4 1561
故里飘歌
故里飘歌 2021-01-03 04:48

I see that hibernate\'s session.get() and load() methods is accepting only Serializable objects.

As per my understanding of hibernate, it will generate an SQL statem

4条回答
  •  梦谈多话
    2021-01-03 04:59

    First of all, the fact that Hibernate uses Serializable in some signature doesn't mean that Hibernate will serialize anything, it just means that parameters are serializable if the need arises.

    Then, I couldn't find an absolute reference but I think that the strongest argument is:

    • Entities Ids are use as key for caching (first level, second level) and may be send over the wire

    Some weaker arguments (or not argument at all):

    • The Session itself can be potentially serialized (e.g. to be stored in the HttpSession)
    • Hibernate needs a super type for entityId (including composite PK)

    Given all this, I think it makes sense to enforce users of the API to pass a Serializable entityId, this allows to not close any door and to avoid any later limitation (oops, you can't activate second level caching because this pk is not Serializable). This is IMO a much better design decision than using Object. And to be honest, I do not see any annoyance with that.

提交回复
热议问题