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!
Related
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
I want to get rid of xml table mappings from my hibernate.cfg and replace them with references to classes themselves.
Here is what is used to have in my hibernate.cfg and what i'm replacing it with..
<mapping class="com.eugene.repository.mappings.Users"/>
<!--<mapping resource="/mappings/Users.hbm.xml"/>-->
I can do that and then run tests from my local box and everything works well,
but when i create war file and upload it to tomcat, it cant resolve mappings..
What am i missing?
I am new to hibernate, developing my first simple program, I got exception of missing Event.hbm.xml file.
I added it, copying code from the net, the I got Person.hbm.xml missing.
I added it then PhoneNumber.hbm.xml missing.
Am I missing some sort of library.
I am working in eclipse.
Assuming there is hibernate.cfg.xml file used for hibernate configuration, you should check for <mapping resource>. File(s) (.hbm.xml) mentioned with <mapping resource> is must be present in that package if it's not found then it will throw the exception.
So either remove <mapping resource> entry for files that are not preset or add the files.
I am trying to use Hibernate 3.2.5 with Play framework 1.2.5
In Hibernate I am having two files:
1) cfg.xml file (containing the db config details along with some additional properties
2) hbm.xml file (containing the mapping between the java bean and the db table
For getting connected to the oracle 10g db, I am providing the db details in the application.config files like this and the connection is successful also when I start the server:
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=system
db.pass=tiger
I want to know Where will I place the hbm.xml file (for mapping details) and the cfg.xml file for the remaining properties other than db connecion details?
Please let me know about this.
Regards,
Starting from the root directory of your application:
the hibernate.cfg.xml must be placed inside the app directory
the mapping files (the hbm files) where your models classes are defined, usually inside the app/models/ directory
Inside your hibernate.cfg.xml the mapping attributes should be something like:
<mapping class="models.yourHmbFile1"/>
<mapping class="models.yourHmbFile2"/>
Btw, I find easy to use the hibernate annotations instead of the hbm - xml mapping. Easier to write and to mantain.
If you prefer to annotate your model classes, you can delete the hbm files and directly map your annotated classes in your hibernate.cfg.xml.
In the application.conf you've to specify the data you have already added:
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=system
db.pass=tiger
Also in the hibernate.cfg.xml you need to specify the connection data:
<property name="hibernate.dialect">...</property>
<property name="hibernate.connection.driver_class">...</property>
<property name="hibernate.connection.url">...</property>
<property name="hibernate.connection.username">...</property>
<property name="hibernate.connection.password">...</property>
I am using classes that are a in referenced project as the mapping for hibernate project - I am able to use the classes in Java code but cannot refer to the classes in the hibernate config file. I get WARN: HHH000183: no persistent classes found for query class
so in the hibernate mapping config file I am using:
...
<mapping package="com.me.my.proj.oracle"/>
<mapping class="com.me.my.proj.oracle.WDI_HBD_PSNG_SMRY"/>
</session-factory> ###end of hibernate config file###
Is it possible to reference a class in a reference project using hibernate?
Your referenced project (Java) is added as JAR file in your current project's classpath. I think you need to use mappingJarLocation with location as your ReferenceProjectName.jar.
<property name="mappingJarLocations" value="file:**/ReferenceProjectName.jar"/>