Hibernate dynamic table name ing-unexpected and impossible

Source: Internet
Author: User

Hibernate dynamic table name ing-unexpected and impossible

There is a demand for a recent project, with N assessment units generating an assessment form for each assessment unit. The purpose of this is to cut the database, my team members encountered a technical problem. I will share my solution and the entire thinking process with you,


Ideas:

Use a configuration file and a class to map multiple tables (each table has the same structure ). According to the usual practice, it is not very troublesome to write as many configuration files as tables. How can I write only one configuration file to achieve the above purpose? After research, we found that NamingStrategy in Hibernate can achieve this goal. It is an interface used to define the ing rules of table names and column names. We need to implement this interface to implement our own naming policy. This interface contains ten methods. The public String classToTableName (String className) is mapped to the table name through the class name. We need to use this method to implement our ideas. Okay, let's see how it works:


Step: 1. Customize a class MyNamingStrategy to implement NamingStrategy. (In this way, you need to implement 10 methods. If other methods are not required, we can only implement the required methods by inheriting one of its adapter classes DefaultNamingStrategy, let's inherit DefaultNamingStrategy.


2. Implement the public String classToTableName (String className) method to implement your own naming policy.


My business needs to change a table every other month. For example, biz_1 was used in January, and biz_2 was used in February .... However, these tables have the same structure. What we need to do is to dynamically select a table by obtaining the month. In this method, we write:


/*** This class is to redefine a hibernate naming convention **/public class MyNamingStrategy extends DefaultNamingStrategy {// This is to implement the singleton public static final MyNamingStrategy INSTANCE = new MyNamingStrategy (); // redefine the name ing relationship public String classToTableName (String className) {// define your own name rule return "biz _" + Calendar. getInstance (). get (Calendar. DAY_OF_MONTH );}}



Now, you can dynamically select the table name based on the month.


3. Use a naming policy.


You can use this naming policy as follows:


Configuration cfg = new Configuration() .setNamingStrategy(MyNamingStrategy.INSTANCE) .configure(“hibernate.cfg.xml”).addFile(“biz.hbm.xml”); 






OK, so that we can implement dynamic table name ing!


Summary:

Most of the time, I am often bound by my own thinking. This is my kind of thinking inertia, and as long as I find a way to solve the problem, I will use it as a collection of collections, if others are better than you, there will also be a kind of rejection information. The two are the cages that impede our progress. We should break through this cage and we should be more bold, think of and see our different side!

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.