Hibernate dynamic Table Name Mapping

Source: Internet
Author: User

Citation: http://blog.csdn.net/xvshu/article/details/39187779

A recent project has a demand, there are n assessment units, to each unit of assessment to generate a checklist, the purpose of this is to cross-cutting the database, this requirement of implementation, my team members encountered a technical problem, I will my solution and the whole thinking process with you to share,

Ideas:

With a configuration file, a class is used to map multiple tables (each table has the same structure). According to the usual practice, how many tables will have to write how many configuration files, it is not very troublesome. How can I write a single configuration file to achieve this? After research, it is found that namingstrategy in hibernate can achieve this goal. It is an interface used to define the table name and column name Mapping rules. We are going to implement our own naming strategy by implementing this interface. This interface contains 10 methods, where the public string Classtotablename (string className) is mapped by the class name to the table name. This is the way to implement our ideas. OK, here's how to do it:

Steps:

1, customize a class mynamingstrategy to achieve namingstrategy. (so you want to implement 10 methods, if other methods do not need, we can inherit one of its adapter class Defaultnamingstrategy to implement only the method we need) Well, let's inherit defaultnamingstrategy.


2. Implement the public string Classtotablename (String ClassName) method to implement its own naming policy.


My business needs to change a table every one months. For example, January with Biz_1, then February with biz_2 .... But the structure of these tables is the same. All we have to do is to select the table dynamically by getting the month. We write this in this way:

[Java]View PlainCopy

/**

* This class is to redefine a hibernate naming convention

**/

public class mynamingstrategy  extends defaultnamingstrategy {   

    1. This is to achieve a single case
    2. Public static final Mynamingstrategy INSTANCE = new Mynamingstrategy ();
    3. Redefine Name Mapping relationships
    4. public string Classtotablename (string className) {
    5. Define rules for your own name
    6. Return "Biz_" + calendar.getinstance (). get (Calendar.day_of_month);
    7. }
    8. }



OK, so you can choose the name of the table dynamically according to the month.


3. Use a naming policy.


To use this naming policy, you can :

Configuration cfg = new configuration ()

. Setnamingstrategy (Mynamingstrategy.instance)

. Configure ("Hibernate.cfg.xml")

. AddFile ("Biz.hbm.xml");







OK, so you can implement our dynamic Table name Mapping!

Summarize:

Many times, I am often constrained by their own thinking, this is my thinking inertia, and I just find a way to solve the problem, will be as a collection of treasure, others have better than you, there will be a kind of rejection of the information, the two are obstacles to our progress, we should break through the cage, we should be more bold, Think and see us on a different side than before!

Hibernate dynamic Table Name Mapping

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.