Can I Specify hibernate annotated classes in hibernate.properties? - java

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.

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

Hibernate 3.2.5 with Play Framework 1.2.5

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>

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

Changing location of c3p0.properties and hibernate.properties

I have a problem regarding the use of hibernate.properties and c3p0.properties. I am dependent upon an API. The API uses Hibernate. As such the API has it's own hibernate.properties and c3p0.properties file included within its delivery jar. I need to ensure I ignore these properties completely when instantiating my Hibernate SessionFactory. I do need these files to remain available on the CLASSPATH so that the API I depend upon can be configured. I simply need a way to instantiate my org.hibernate.SessionFactory so that it doesn't look at these files in any way. I don't mind using an xml configuration or setting my Hibernate properties manually. I just need to ensure that the properties in hibernate.properties and c3p0.properties are not read.
In order to do this you will have to use an XML hibernate config file. If both the hibernate.properties and hibernate.cfg.xml are present, the XML file will override the properties file.
You can find more information here:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-xmlconfig
Note: your c3p0 properties can be defined in the same XML file.

Categories