What if you need to switch scheme when calling hibernate?
In Oracle, different users use different schemas. In the Pojo of hibernate, a schema is specified
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<class name="com.csc.poimanager.dao.Poi" table="POI" schema="P_BEIJING">
<id name="poiId" type="java.lang.Long">
<column name="POI_ID" precision="10" scale="0" />
<generator class="increment" />
</id>
<property name="cnName" type="java.lang.String">
<column name="CN_NAME" length="1000" />
</property>
</class>
In the code section above, the schema is specified. If you want to switch schemas while you are working, you can do the following:
Default configuration <property name= "Hibernate.default_schema" >POI_BEIJING</property>
The mapping file above should read:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<class name="com.csc.poimanager.dao.Poi" table="POI" >
<id name="poiId" type="java.lang.Long">
<column name="POI_ID" precision="10" scale="0" />
<generator class="increment" />
</id>
<property name="cnName" type="java.lang.String">
<column name="CN_NAME" length="1000" />
</property>
</class>
In action, you can use the following method to rebuild your sessionfactory
public static void rebuildSessionFactoryForChangeSchema(String newSchema){
try {
Properties p = configuration.getProperties();
System.out.println("---" + p);
p.put("hibernate.default_schema", newSchema);
sessionFactory = configuration.buildSessionFactory();
System.out.println(" change schema successfully ......... ");
} catch (Exception e) {
System.err
.println("%%%% rebuild session factory failed for changing schema %%%%");
e.printStackTrace();
}
}
If you need to change the schema, you need to call this method when you need it
Like in the schemaaction.
HibernateSessionFactory.rebuildSessionFactoryForChangeSchema("POI_SHANGHAI");
System.out.println(" change successfully ---");
PoiDAO pd = new PoiDAO();
Transaction t =pd.getSession().beginTransaction();
pd.save(new Poi("jsfjksdf"));
t.commit();
So, the original, is to insert the data into the poi_beijing, into the Poi_shanghai to insert a piece of data.
In this way, you can implement a switch when manipulating a different schema.
Problem: The static factory is changing here. Therefore, all users will have an impact. If you do not want to change all users, then you can according to the name of the schema to get their corresponding sessionfactoy.