Cainiao SSH (18)-Hibernate dynamic model + JRebel implement dynamic table creation, sshjrebel

Source: Internet
Author: User

Cainiao SSH (18)-Hibernate dynamic model + JRebel implement dynamic table creation, sshjrebel

The project uses the basic SSH framework. Some of the information is similar, but not the same. If each entity is created, there will be too many entities. If groups are abstracted and inherited, it is not particularly regular. In this case, we plan to allow the user to configure the fields to be added and then generate the corresponding table.


Some instances that require dynamic configuration:



It's only a small part. There are more than three hundred combinations. Each item corresponds to an entity, which is obviously not good. Even if there are still a lot of objects classified by law, we thought of determining these things at runtime. At first, some attempts were made to dynamically compile and generate entity classes, which were later found to be problematic in Data Access. Because they were generated later, they can only be obtained through reflection. As a result, the types cannot be determined in advance, in this way, you cannot receive data transmitted from the front end in the injection mode, nor can you provide data to the front end. Later, I decided to use the dynamic model of Hibernate to solve this problem. Some people may not be very familiar with the dynamic model of Hibernate. Let's introduce it below.


We usually use entity classes to map to tables. When we need a user table, we usually need to write an object 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 annotation to describe the ing relationship. If a dynamic model is used, you do not need to write entity classes. You only need to write a configuration file:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

Then perform operations through Map:

Session. beginTransaction (); // Map the object to the database Map user = new HashMap (); user. put ("name", "Dynamic Model"); user. put ("password", "123456"); session. save ("User", user); session. getTransaction (). commit (); session. close ();

SQL statement: insert into User (name, password) values (?, ?)


Some people say that it is very convenient to store dynamic ing in data, but it seems difficult to extract data from the data. In fact, this problem can be solved through prior Conventions.


The previous blog introduced JRebel, which enables Tomcat to support hot deployment. JRebel + Hibernate dynamic models can be combined to meet our dynamic table creation requirements.


Add the following to the Spring configuration file:

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

Use wildcards to configure hbm. the xml file is designed to be compatible with the dynamic model configuration file generated during the runtime. JRebel can detect the changes in the configuration file and load the new configuration, it should be noted that the dynamic loading of JRebel is a lazy loading. That is, the modified content will be reloaded only when you use the modified content.


I am also using the Hibernate dynamic model for the first time. At present, it is a trial phase. If anyone has used it or is interested in the dynamic model, please leave a message.




In the SSH architecture, how does Hibernate dynamically query different tables?

Hibernate Criteria can be used for self-use Query
You can use this to write a general Service, set the queried entities, fields, and sorting rules, and then call


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.