Dynamically switching schema in Hibernate

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.