Can hibernate scan packages to create SessionFactory automatically?
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).
You don't want to use Spring, so what can you do:
Using fluent-hibernate
You can use
EntityScanner from fluent-hibernate library (you will not need to have other jars, except the library)
For Hibernate 4 and Hibernate 5:
Configuration configuration = new Configuration();
EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
.addTo(configuration);
SessionFactory sessionFactory = configuration.buildSessionFactory();
Using a new Hibernate 5 bootstrapping API:
List<Class<?>> classes = EntityScanner
.scanPackages("my.com.entities", "my.com.other.entities").result();
MetadataSources metadataSources = new MetadataSources();
for (Class<?> annotatedClass : classes) {
metadataSources.addAnnotatedClass(annotatedClass);
}
SessionFactory sessionFactory = metadataSources.buildMetadata()
.buildSessionFactory();
Using other libraries
If you already use a library that can be used for scanning, for an example Reflections, there is a test project with examples of using various libraries for entity scanning: hibernate-scanners-test.
Mapping all the classes in hibernate.cfg.xml with just one line?
You can't do it using Hibernate only.
You can use Spring with org.springframework.orm.hibernate5.LocalSessionFactoryBean
(package hibernate5
for Hibernate 5, change it if need) or additional libraries as described here:
Hibernate config not list all Entities in XML
MappingException with hibernate.cfg.xml
Place Movie.hbm.xml
under src/main/resources
and change path in your config file as follows,
<mapping resource="Movie.hbm.xml" />
Hope this helps.
how to map classes in hibernate using the class attribute?
I should add this:
configuration.configure("hibernate.cfg.xml");
return configuration
.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build());
to my HibernateUtil Class to tell that we want to configure using the hibernate.cfg.xml file and map the classes like this:
<mapping class="com.redpass.entities.Partie"/>
<mapping class="com.redpass.entities.Societe"/>
Related Topics
How to Convert a Binary String to a Base 10 Integer in Java
How to Serialize Only the Id of a Child with Jackson
How to Configure Log4J to Log Different Log Levels to Different Files for the Same Logger
Running Selenium Scripts with Jmeter
How to Use the Jersey JSON Pojo Support
Log4J2 - Assigning File Appender Filename at Runtime
Compile Error: Package Javax.Servlet Does Not Exist
How to Pass C Structs Back and Forth to Java Code in Jni
Are There Any Other Java Libraries for Bonjour/Zeroconf Apart from Jmdns
User Authentication on a Jersey Rest Service
How to Add Test Coverage to a Private Constructor
Convert Bufferedinputstream into Image
Can Add Extra Field(S) to @Manytomany Hibernate Extra Table
String Replacement in Java, Similar to a Velocity Template
How to Solve the "A Generic Array of T Is Created for a Varargs Parameter" Compiler Warning
Differencebetween Putting a Property on Application.Yml or Bootstrap.Yml in Spring Boot