'Hibernate can't find properties file (even though it exists)
So I'm working on a Java Maven project in VS Code and decided to use "raw" Hibernate (no JPA) and MySQL to handle the database. According to the documentation, you can specify the configuration settings in a XML file (hibernate.cfg.xml) placed in the resources folder (src/main/resources), which is what I'm currently doing. However, this approach doesn't seem to work for me:
SessionFactory sessionFactory = new Configuration()
.configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
mar 20, 2022 5:35:37 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.16.Final}
mar 20, 2022 5:35:38 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
mar 20, 2022 5:35:38 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;)V
at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.generateDefaultReflectionManager(MetadataBuilderImpl.java:742)
at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:715)
at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127)
at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:654)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at tfg_project.HibernateDemo.main(HibernateDemo.java:13)
Even if I try to indicate the path to the configuration file via the configure method, Hibernate will not find it. I've tried moving it to different folders while changing the method input, even in the same folder as the running code, but it won't matter. I've also tried inputting the configuration programmaticaly to no avail.
The only hint I have is about the classpath: I tried to run the code from the resources folder which prompted this error So I added the folder to the classpath with Maven and apparently, the folder is in the classpath, but code there still isn't executable. This might be causing the error, but then it wouldn't matter because I tried moving the config file to other folders.
I've spent several hours trying to fix this, and I'm sure it's just a small error I'm not taking into account, but I ran out of ideas on how to tackle it. Any ideas are appreciated.
Solution 1:[1]
The solution I came up with was to use JPA and delete the config file (JPA uses a different one). I followed this tutorial: https://www.javahelps.com/2016/10/jpa-hello-world-using-hibernate-5.html
Apart from a couple of errors with configuration, the change was pretty straightforward and it actually works. I was expecting a similar problem with the resources path, but for some reason JPA could actually find its configuration file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gonzalinho |
