Hibernate 4 Annotation Configuration

I am trying to use Hibernate 4 only with annotations and hibernate.cfg.xml . I made my own annotation and use reflection to add this to the configuration. I can use Hibernate 4 in this way, but my configuration is built using an obsolete method.

 final Configuration configuration = new Configuration(); final Reflections reflections = new Reflections(Item.class.getPackage().getName()); final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class); for (final Class<?> clazz : classes) { configuration.addAnnotatedClass(clazz); } return configuration.configure().buildSessionFactory(); 

(Deprecated code: buildSessionFactory(); ).

Even the hibernate 4 documentation shows how to build a configuration this way.

If I try to use the new method ( buildSessionFactory(ServiceRegistry) , I do not get the same result, and it seems that a lot of unnecessary code does exactly what the obsolete method does anyway. However, I do not want to continue using this style because I don't like using outdated code.

My question is: how to properly configure Hibernate 4 only from the configuration file as described above? I seem to just cause errors and run into unnecessary difficulties.

+7
source share
1 answer

The modified code will look like this: -

  final Configuration configuration = new Configuration(); final Reflections reflections = new Reflections(Item.class.getPackage().getName()); final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class); for (final Class<?> clazz : classes) { configuration.addAnnotatedClass(clazz); } ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings (configuration.getProperties()).buildServiceRegistry(); return configuration.buildSessionFactory(serviceRegistry); 

You can check the following links for information: HHH-6183 and HHH-2578 .

+9
source

All Articles