views:

69

answers:

2

Hi, I have a HibernateUtil class which is the standard by the book (Hibernate in action) class.
It gets a session, flushes and closes it. begins,commits and rollbacks transactions and build the sessionFactory.
Currently, by mistake, this is called HibernateSessionFactory in our package.
I want to refactor it to a more appropriate name but the name HibernateUtil bothers me as I'd like to choose a more general name something like PersistentManagerUtil.
Is there a good java convention or consensus about such a name?

EDIT The reason I want to make it more general is that I want to minimilize the "Exposure" to the specific ORM implementation. 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. Why not change it to EntityManagerUtil? As in hibernate's docs they say EntityManager is equivelant to Session and that EntityManagerFactory is equivelant to HibernateSessionFactory.

Thanks in advance,
Ittai

+2  A: 

Don't make it more general, unless the class can really do anything other than talk to Hibernate. It is using Hibernate classes and interfaces in its public API, right? As long as it is Hibernate-specific (in both interface and implementation), it would be good to let everyone know about it in its name.

HibernateUtil sounds good.

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.

Well, you can change the name to JpaUtil once you got rid of getSession.

Thilo
Hi, I started to comment but became to long, I'm updating my question
Ittai
I think I will change to JpaUtil once I change getSession to getEntityManager. Thanks.
Ittai
A: 

Your class is Hibernate specific, so it's good to have that in its name.

Does it create Sessions, or get them from elsewhere? If it creates them, Factory is also a good name. If not, it sounds like it manages Sessions so how about 'HibernateSessionManager'?

Your name 'HibernateUtil' is if anything too general. I always take 'util' packages and classes as a code smell; they usually pop up because the designer had a bunch of stuff he/she did not know where to put. Your class is not like that, it apparently focuses on the management of hibernate sessions.

As to your question about existing conventions or consensus about naming. There are some guidelines (but you probably already know them):

  • methods should reflect an action on the class they reside in and if possible have a logical argument order. Example: ChessBoard.swapPlayers(Player a, Player b)
  • for methods returning a boolean use a predicate like isActivated or hasPlayers
  • another style is method chaining, where the chained methods read a bit like an english sentence: verify(mockedList, atLeastOnce()).add("three times"); (mockito)
  • The JavaDoc on methods should describe the action, for example: swaps players A and B so that they continue the game with each others' pieces".
  • classes should represent a 'thing' with a clear responsibility. Examples: StringBuilder, Customer, PdfServlet. If a secondary responsibility creeps into a class, this usually means it should be refactored into two collaborating classes.
  • JavaDoc on classes should describe the 'thing' and its main responsibility. Behavior should be described insofar it is valuable information for the client. Implementation details should usually not be described in the public JavaDoc.

Oh yes, and I happen to think naming is extremely important for good software design. But I guess that was already clear (-:

EDIT: in response to your edit. As long as it returns a Hibernate type, it is still Hibernate specific. If you manage to totally abstract away from Hibernate, towards JPA, then it would make sense to name it JpaSessionManager (or JPASessionManager if you prefer) for example. But this would require to return a JPA-only session object.

Adriaan Koster
@Adriaan, Thanks for the naming guidelines but I was reffering to hibernate and JPA. I think my terms were mixed up but now I've read a bit more and I understand I just need to switch to JPA and then this issue will be resolved by definition.
Ittai
@Adriaan, I'm actually having second thoughts about the "UTIL" issue. I'm thinking of separating the class to 2 classes. One which will be EntityManagerFactory which will generate sessions (but it will need to also close and flush them, is this compatible with Factory design?) and the second which will be UnitOfWorkEngine which will be responsible for begining,commiting and rollbacking transactions. What do you think? looking forward to hearing your opinion
Ittai
About 'EntityManagerFactory': does it handle Sessions or Entities? Does it manage or create them? This is not clear from your name. About 'UnitOfWorkEngine' : Sounds quite vague. If it handles transactions, why not call it TransactionManager (or -Engine although this suggests a lot more work than just lifecycle management)?Spring has a class called 'HibernateTemplate' which is basically a 'util' type class which offers similar functionalities. Not that I particularly like this name, but worth a thought.
Adriaan Koster
In JPA the Hibernate Session is EntityManager and the Hibernate Transaction is UnitOfWork so I'm trying to use the names of the relevant interaces in JPA. Basically I think my question has transformed and I'm asking: 1)MyClassFactory is good when it also destroys the MyClass? 2)How would you call the lifecycle management class? thanks
Ittai