Dynamic hibernate mapping file
Here is an example of my mapping file:
<!-- ============================ -->
<!-- Table TABLE1 -->
<!-- ============================ -->
<class table="TABLE1"
name="com.myCompany.Entity1" lazy="false" schema="SCHEMA1">
<!-- Attributs -->
<id column="ID" name="id" type="string" />
<property column="ACTION_TYPE" name="actionType"
type="com.myCompany.ActionEnumType" not-null="true" /开发者_运维问答>
<property column="PRIORITY" name="priority"
type="com.myCompany.PriorityEnumType" not-null="true" />
<property column="DATE_MAJ" name="dateMaj" />
</class>
<!-- ============================ -->
<!-- Table TABLE2 -->
<!-- ============================ -->
<class table="TABLE2"
name="com.myCompany.Entity2"
lazy="false" schema="SCHEMA2">
<!-- Attributs -->
<id column="ID" name="id" type="string" />
<property column="ACTION_TYPE" name="actionType"
type="com.myCompany.ActionEnumType" not-null="true" />
<property column="PRIORITY" name="priority"
type="com.myCompany.PriorityEnumType" not-null="true" />
<property column="DATE_MAJ" name="dateMaj" />
</class>
<!-- ============================ -->
<!-- Table TABLE3 -->
<!-- ============================ -->
<class table="TABLE3"
name="com.myCompany.Entity3"
lazy="false" schema="SCHEMA3">
<!-- Attributs -->
<id column="ID" name="id" type="string" />
<property column="ACTION_TYPE" name="actionType"
type="com.myCompany.ActionEnumType" not-null="true" />
<property column="PRIORITY" name="priority"
type="com.myCompany.PriorityEnumType" not-null="true" />
<property column="DATE_MAJ" name="dateMaj" />
</class>
The goal, is to put all the data to be configurable in one external file. The purpose is to define the schema name dynamically because it depends on the deployment environment. Unfortunetely, we can't use in our project neither Maven nor Ant. How or where can i set the different schemas name to resolve this issue?
Thx in advance for you help.
No, there is no support for such thing, so using a tool like Maven or Ant for dynamically setting this value based on a properties file is the way to go.
There is no support for this in Hibernate. However we use Ant to achieve this with help of environment specific property files.
Hibernate supports defining a default catalog for unqualified table names in the configuration files (either in hibernate.cfg.xml
or in hibernate.properties
) using the hibernate.default_schema
property. From the documentation:
3.4. Optional configuration properties
...
hibernate.default_schema: Qualify unqualified table names with the given schema/tablespace in generated SQL. e.g. SCHEMA_NAME
Depending on the complexity of your use case, this might help (or not). If it doesn't, I'm afraid you'll have to use some kind of filtering approach (Ant also offers filtering) - or to patch Hibernate.
精彩评论