I've tried to indicate a relative path to hibernate get the hibernate.cfg.xml but it does not work, because I've wrote that wrong (i've typed cgf instead of cfg in the file's name). So I've tried an absolute path but Hibernate doesn't recognize the new path, and still looking at the relative path that I've passed before. But the parameter .configure() has chenged, why does Hibernate ignore and insists in mistake?
My code:
private static SessionFactory buildSessionFactory() {
Configuration configuration = new Configuration();
// Use the mappings and properties specified in an application resource named hibernate.cfg.xml.
configuration.configure("C:\\Users\\Lucas_Pletsch\\eclipse-workspace\\PDV\\main\\resources\\hibernate.cfg.xml");
The error screen showing that Hibernate searched for hibernate.cfg.xml in the path that I've passed as parameter before:
Now I've tried this:
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
And the error message have changed, but Hibernate still not finding .cfg.xml:
Try updating your maven project: RightClick > Maven > Update Project.
If issue still persists place hibernate.cfg.xml file under the src folder.
Your hibernate.cfg.xml file is already part of classpath. you don't need to specify complete path
Try below code :
SessionFactory sessionFactory = new Configuration().configure(
"hibernate.cfg.xml")
.buildSessionFactory();
Update your project as: Right click on the project->select Maven->then select Update project
Related
I am trying to externalize my ignite configuration in my spring boot application so the configuration can be changed without rebuilding the jar.
Previously the file resided in src/main/resrouces and was loaded via annotations.
#ImportResource("IgniteConfig.xml") and
#Autowired
private IgniteConfiguration cfg;
When I moved the IgniteConfig.xml to the config folder that resides next to the excutable jar the above stopped working and I have tried the following without success:
use --spring.config.location argument. I can tell this is picked up during run time as other configurations work but the above ImportResource annotation says the file IgniteConfig.xml cannot be found.
use a relative path to (e.g. ./config.IgniteConfig.xml) to Ignition.start. I cause this relative path to print the file contents of the xml file in my logs but when I pass it to Ignition.start it says the file cannot be found. I have tried using relative and absolute paths to do this.
Manually create an ApplicationContext and get the configuration by bean name.
ApplicationContext context = new ClassPathXmlApplicationContext("./config/IgniteConfig.xml");
This again complains that the file does not exist even though I can see by opening the file directly:
File igniteConfigFile = new File("./config/IgniteConfig.xml");
The comment by konqi in this post answered my question:
"In case you want to import a resource that is outside the classpath the syntax would be:
#ImportResource( { "file:path/spring-context1.xml", "file:path/spring-context2.xml" } )
"
In my case I just needed to do:
#ImportResource( { "file:./config/IgniteConfig.xml" } )
I built my project using the Eclipse IDE, and placed my hibernate.cfg.xml file in the resources folder (screenshot below):
The problem occurs when I try to reference hibernate.cfg.xml in my DAO classes. Here is a snippet of my code where I get my SessionFactory:
DAO getSessionFactory() example
private static SessionFactory getSessionFactory() {
String hibernatePropsFilePath = "src/main/resources/hibernate.cfg.xml";
File hibernatePropsFile = new File(hibernatePropsFilePath);
Configuration configuration = new Configuration();
configuration.configure(hibernatePropsFile);
configuration.addAnnotatedClass(MyClass.class);
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
return configuration.buildSessionFactory(serviceRegistry);
}
As you can see, I'm still pointing to the "correct" location in my project, however when I test using Postman, I get the following error:
HTTP Status 500 - Request processing failed; nested exception is
org.hibernate.internal.util.config.ConfigurationException: Specified
cfg.xml file
[C:\Users\MYUSERNAME\Desktop\src\main\resources\hibernate.cfg.xml] does
not exist
My question: Why is it constantly looking in my Desktop for my file path, and how do I change it to look only in my project? I checked and made sure that my resources path is present in my Web Deployment Assembly (screenshot below):
src/main is maven specific project structure to arrange the source files. But after build the compiled classes and resource files are copied to WEB-INF/classes/ directory.
Assuming you are using maven and your resources folder is part of the source structure, then the XML file will be copied to WEB-INF/classes/resources folder.
Change the path to String hibernatePropsFilePath = "/resources/hibernate.cfg.xml";
Update:
After taking a second look at your Web Deployment Assembly settings image, your main/resources folder is mapped to classes directory not classes/resources. so change the code as shown below without use of File.
Configuration configuration = new Configuration("hibernate.cfg.xml");
configuration.addAnnotatedClass(ClientCrossRef.class);`
configuration.configure();
and even more better thing is, since your cfg.xml file is already part of classpath, you don't even need to specify it. Hibernate will look for it in the classpath.
And by the way, you can build the session factory easily as
SessionFactory sessionFactory = new Configuration().configure(
"hibernate.cfg.xml")
.buildSessionFactory();
Is it possible to move a hibernate configuration file containing the db connection details outside of the hibernate project and connect to it?
My current configuration is as follows, but I would like to move my file from outside the project and still connect to my data sources. How can I do so?
#Configuration
#EnableTransactionManagement
#PropertySource({"classpath:hibernate.properties"})
#EnableJpaRepositories(basePackages = "com.my.packages", entityManagerFactoryRef = "schemaOneManagerFactory", transactionManagerRef = "schemaOneTransactionManager")
public class SchemaOneDataSourceConfig
{
//configuration methods
}
Do I need to make a change to line: #PropertySource({"classpath:hibernate.properties"}) ?
From the JavaDocs of PropertySource
Indicate the resource location(s) of the properties file to be loaded. For example, "classpath:/com/myco/app.properties" or "file:/path/to/file".
You just need to change a prefix to file:
#PropertySource({"file:/path/to/hibernate.properties"})
If you are using Spring Boot, you can put the properties in application.properties and you can have that file in your JAR, but also override it by putting it in a config sub-folder (relative to your running directory).
See http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files for more info.
I have just started to learn the Hibernate and found this in various online sites : mapping.xml and config.xml has to be defined outside the pojo package?
Why is that so?
Also what's the difference between JPA and Hibernate. I searched through web and According to me hibernate is just one of the implementation of JPA. Could u correct me.
I have just started to learn the Hibernate and found this in various online sites : mapping.xml and config.xml has to be defined outside the pojo package?
You can put xml configurations whenever you want. For an example
SessionFactory factory = new Configuration().configure().buildsessionFactory();
Configure a session factory from the default hibernate.cfg.xml. It is the same as
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml").buildsessionFactory();
So why hibernate.cfg.xml should be in the root of the source folder (or in the resources folder) in this situation?
Hibernate tries to load hibernate.cfg.xml via class loader
InputStream stream = classLoader.getResourceAsStream( "hibernate.cfg.xml" );
if you specify just a name without a path ("hibernate.cfg.xml") a class loader will try to find a resource in the root of the compiled sources folder — bin or build folder, or the classes folder for war. The resources folder after build is copied (for an example, by Maven) in the root of the build or classes folder.
if you specify
new Configuration()
.configure("/some/pojo/hibernate.cfg.xml").buildsessionFactory();
A class loader will try to find a resource in the some.pojo package. In this situation Hibernate removes the leading /, because of for a loading via a class loader the leading / is incorrect. So you can use a code below too
new Configuration()
.configure("some/pojo/hibernate.cfg.xml").buildsessionFactory();
The same rule for other paths to the xml resources.
Also what's the difference between JPA and Hibernate. I searched through web and According to me hibernate is just one of the implementation of JPA. Could u correct me.
Yes, Hibernate is an implementation of JPA. But one thing more. If you use the SessionFactory you can think that you don't use JPA. But in the same time you use JPA annotations (#OneToMany for an example). When you use EntityManager — you use JPA exactly.
I am developping a standalone application with hibernate, using the following stucture for my project :
This is how I told hibernate(yes, I'm chatting with Hibernate) where to find the hibernate.cfg.xml file :
File hibernateConfigurationFile = new File("..\\resources\\hibernate.cfg.xml");
...
Configuration().configure(hibernateConfigurationFile).buildSessionFactory();
This is how I told hibernate where to find the Event.hbm.xml file :
<mapping resource="../resources/test/Event.hbm.xml"/>
I also tried :
<mapping resource="test/Event.hbm.xml"/>
But when I compile it, I have the following error :
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: test/Event.hbm.xml not found
I understand it is because I indicated the wrong mapping file path in my hibernate.cfg.xml file. So I moved my Event.hbm.xml file, and the structure of my project is now :
And all works fine.
My problem is that I would like to keep my Event.hbm.xml in the resource\test directory. How can I indicate that in my hibernate.cfg.xml file? I tried relative paths, but it doesn't work. I read hibernate documentation, but found nothing. Any suggest?
You can try programmatic way of building hibernate configuration and can say
SessionFactory sf = new Configuration()
.addFile("yourpath/Event.hbm.xml")
instead of declaring mapping resource in hibernate.cfg.xml
see this method
I was facing the same problem
I used this
<mapping file=".\hibernate\groupResult.hbm.xml" />
Means there is an attribute file in mapping
.\hibernate\groupResult.hbm.xml
will be in your project folder in that hibernate folder in that groupResult.hbm.xml
Hope this will help you!
Cheers!