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:
<span style= "FONT-SIZE:18PX;" >/** * This class is redefining a Hibernate naming convention **/public class Mynamingstrategy extends Defaultnamingstrategy {//This is to implement singleton public Static final Mynamingstrategy INSTANCE = new Mynamingstrategy (); Redefine the name Mapping relationship public string Classtotablename (string className) {//own name Definition rule return "biz_" + calendar.getinstance (). Get ( Calendar.day_of_month); }} </span>
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:
<span style= "FONT-SIZE:18PX;" >configuration cfg = new Configuration (). Setnamingstrategy (mynamingstrategy.instance). Configure (" Hibernate.cfg.xml "). AddFile (" Biz.hbm.xml "); </span>
OK, so you can implement our dynamic Table name Mapping!
Summary:
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--only unexpected, not done