Home Ask Login Register

Developers Planet

Your answer is one click away!

Gautam February 2016

Can hibernate scan packages to create SessionFactory automatically?

Can I configure Hibernate to scan packages automatically to create a SessionFactory from @Entity annotated beans ?

Currently I am using

Configuration config = new Configuration() ;
config.addAnnotatedClass(MyEntity.class);

I do not want to use hibernate.cfg.xml to configure mappings.

Please note I want to achieve this in a plain Java project without using any Spring or such frameworks.

Similar question have been answered using Spring before but I want to achieve it without using Spring or other frameworks. I am open to some simple library that does this.

Answers


v.ladynev February 2016

No. You can't say Hibernate to scan packages for persistent classes even with the last Hibernate 5 version. Configuration has method addPackage(), but it is for reading "package-level metadata" (.package-info- files).

What can you do

  1. You can copy-paste code from Spring's LocalSessionFactoryBuilder.
  2. You can use ClassPathScanningCandidateComponentProvider from Spring, Reflections library, or custom code — as described in Hibernate Mapping Package.

I have plans to add scanning packages functionality to my simply utilities library for Hibernate too.

Post Status

Asked in February 2016
Viewed 3,930 times
Voted 8
Answered 1 times

Search




Leave an answer


Quote of the day: live life