Hibernate 3.2.5 with Play Framework 1.2.5 - java

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>

Related

How to set property of hibernate.cfg.xml from java class?

I have a web application that read an argument from tomcat -D option and decide which property file should be used. So, How can I set below property in hibernate xml? Here What I want to do:
run tomcat with -Doption=xOption
Singleton Config class load xOption.properties
<hibernate-configuration>
<property name="hibernate.connection.url">Config.getinstance().getValue("dbUrl")</property>
</hibernate-configuration>
I get used to do it with using hibernate java config. Is there anyway to do it in xml file?

Specify a regular expression for LocalSessionFactoryBean mappingLocations

In my application, I have specified the below configuration for automatically picking up all the HBM files under a specific folder in the classpath.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="mappingLocations">
<list>
<value>classpath:hbms/**/*.hbm.xml</value>
</list>
</property>
</bean>
Now, for a new requirement, there is a need to create multiple HBM files with named queries specific to database. The HBM file names will be of the pattern test.DB.hbm.xml. For example, test.oracle.hbm.xml and test.db2.hbm.xml. In addition to these, there are the old regular HBM files (for mapping to tables) with name format as table1.hbm.xml, table2.hbm.xml, etc. also present in the same folder.
Using the above pattern, hibernate loading of the files fails, due to duplicate named queries in the new hbm files (since the name would be same in all such files).
The requirement is now to load the regular HBM files and also the DB specific HBM files. Is it possible to achieve this by using a regular expression as below?
classpath:hbms/**/*.(.${dbType}).hbm.xml
In the above example, dbType is available as a Spring environment property. My attempt with these changes resulted in none of the HBM files being loaded (including the old ones).
Am I doing something wrong with the regular expression or is it not possible to do this via XML configuration?
Thank you.
You have two options:
You can have each database specific config files be stored in a separate folder and then your config looks like this:
classpath:hbms/**/${dbType}/*.hbm.xml
You can have them follow he pattern you provided, but change the configuration to
classpath:hbms/**/*${dbType}.hbm.xml
To load common files, you need to rename them to include something you can match, like:
one.hbm.xml
becoming:
common-one.hbm.xml
Then the configuration might look like this:
classpath:hbms/**/common-*.hbm.xml
classpath:hbms/**/*${dbType}.hbm.xml

Can I indicate mapping file using relative paths in hibernate?

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!

Map a class in hibernate that is in a referenced project

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"/>

Can I Specify hibernate annotated classes in hibernate.properties?

Most or all the core hibernate configuration properties can be specified in a startup properties file, as an alternative to specifying mappings in hibernate.cfg.xml.
Is there an easy way to specify mappings of annotated classes in a properties file?
You can map annotated classes like this in the hibernate.cfg.xml configuration:
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
...
<mapping class="com.annotated.classes.EntityOne"/>
</session-factory>
Similar configuration can be written using a properties file i.e. hibernate.properties
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.mapping = ???
What I haven't figured out is how to specify the mapping of annotated classes (entities) using the properties configuration, if this is possible.
I don't think there is any way to specify entities in the properties file.
I suppose you could create your own custom initialization code using Configuration to accomplish what you are looking for. The problem with using a properties file, though, is that you cannot specify a property with a list of values unless you identify a way to split the value into a list. You would then need to write code that parses the value accordingly.

Categories