我有一个 HibernateUtil 类,它是本书(Hibernate in action
)类的标准。
它获取一个 session ,刷新并关闭它。开始、提交和回滚事务并构建 sessionFactory。
目前,我们的包中这被错误地称为 HibernateSessionFactory。
我想将其重构为更合适的名称,但 HibernateUtil 这个名称让我很困扰,因为我想选择一个更通用的名称,例如 PersistentManagerUtil
。
关于这样的名字有一个好的java约定或共识吗?
编辑 我想让它更通用的原因是我想最大限度地减少对特定 ORM 实现的“暴露”。该类的大多数公共(public)方法都将 void 作为返回类型,因此它们不返回 Hibernate 类/接口(interface)。唯一的异常(exception)是 getSession,它返回 org.hibernate.Session。我之所以考虑这个方向是因为我想切换到JPA并使用hibernate作为实现,因此有更多的自由度。 为什么不将其更改为 EntityManagerUtil?就像在 hibernate 的文档中一样,他们说 EntityManager 相当于 Session,EntityManagerFactory 相当于 HibernateSessionFactory。
提前致谢,
伊泰
最佳答案
不要让它变得更通用,除非这个类除了与 Hibernate 对话之外真的可以做任何事情。它在其公共(public) API 中使用 Hibernate 类和接口(interface),对吧?只要它是 Hibernate 特定的(在接口(interface)和实现方面),最好通过其名称让每个人都了解它。
HibernateUtil
听起来不错。
Most of the public methods of the class have void as a return type so they do not return Hibernate classes/interfaces. The only exception to this is the getSession which returns an org.hibernate.Session. The reason I was thinking this direction is because I want to switch over to JPA and have hibernate as the implementation and so have a bit more freedom.
好吧,一旦摆脱了getSession
,您就可以将名称更改为JpaUtil
。
关于java - HibernateUtil 类的一个好的替代通用名称是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3625197/