Rookie learn SSH (17)--hibernate Dynamic Model +jrebel implement dynamic creation table

Source: Internet
Author: User

The project uses the SSH infrastructure, some of which are similar, but different. If each entity is built, then there will be too many entities, and if the grouping is abstract and then inherited, it is not particularly regular. In this case, you intend to have the user configure the fields to be added themselves, and then generate the corresponding table.


Partial instances that require dynamic configuration:



Just a small part, a combination of about 300 more. Each item corresponds to an entity, which is obviously not good, even if it is classified according to the law or a lot, so I think in the run time to determine these things. began to have tried to generate dynamic compilation of entity classes, and later found that there are problems in data access, because it is generated later, so can only be obtained by reflection, so it is not possible to determine the type in advance, it can not be injected to receive the data from the front end, and can not provide data to the front end. Later, I decided to use Hibernate's dynamic model to deal with this problem, perhaps some people are not very familiar with hibernate dynamic model, let's introduce.


We usually use entity classes to map to tables, and when we need a user table, we usually need to write an entity class similar to the following:

 Public class User {    private  Long ID;     Private String name;     Private String password;         // Setter, getter ...    }


Then use the configuration file or annotations to describe the mapping relationship, if you use a dynamic model, you do not need to write the entity class, only need to write a configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "><hibernate-mapping>    <!--class name, and package name are not required here -      <classEntity-name= "User">        <IDname= "id"type= "Java.lang.Long"column= "ID">            <Generatorclass= "Native"/>        </ID>        < Propertyname= "Name"type= "Java.lang.String"column= "Name"/>        < Propertyname= "Password"type= "Java.lang.String"column= "Password"/>    </class></hibernate-mapping>


Then work through the map:

session.begintransaction (); // map entities and databases through map New HashMap (); User.put ("name", "Dynamic Model"), user.put ("password", "123456"); Session.save ("User" , user); Session.gettransaction (). commit (); Session.close () ;

Issued SQL statement: INSERT into User (name, password) VALUES (?,?)


Some people say that it is convenient to store data dynamically, but extracting data from the data seems to be difficult to deal with, in fact this problem can be solved by making a good deal beforehand.


The previous blog introduced Jrebel, which allows Tomcat to support hot deployment. Jrebel+hibernate dynamic model with two swords, we can achieve the requirements of dynamic building table.


In the spring configuration file, add:

<name= "mappinglocations">      <list  >          <value>classpath:/com/tgb/entitycfg/*.hbm.xml</  value>      </list>  </  Property>


The use of wildcard characters to configure the Hbm.xml file, is to be compatible with the runtime generated dynamic model configuration files, and Jrebel can detect changes in configuration files, so that the new configuration to load in, it should be noted that the dynamic loading of Jrebel is lazy loading, that is, when you use the modified thing is, will be your modified content reloaded into To.


I am also the first to use the hibernate dynamic model, is now a trial stage, if you who have used or interested in dynamic models welcome message exchange.



Rookie learn SSH (17)--hibernate Dynamic Model +jrebel implement dynamic creation table

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.