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

Source: Internet
Author: User

The project uses the SSH infrastructure, and some of the information is very similar, but not the same. Assuming that each entity is built, there will be too many entities. It is not particularly regular to assume that group abstractions are then inherited. In view of this situation. You are going to let the user configure the fields to be joined, and then generate the corresponding table.


Some instances where dynamic configuration is required:



is just a small part, one by one together about more than 300. Each corresponding entity is obviously not good, even if it is classified according to the law or a lot. So I thought of the implementation period to determine these things. There have been attempts to generate entity classes by Dynamic compilation, and later there are problems with data access, which is generated later, so it can only be obtained by reflection. This makes it impossible to determine the type beforehand, and it is not possible to ingest the data from the front end or to provide the 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 the dynamic model hibernate, we will introduce the following.


We often use entity classes to map to tables. When we need a user table, we usually have 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 narrative mapping relationship, assuming that the use of dynamic model, you do not need to write the entity class, just need to write a configuration file can be:

<?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 ">

then work through the map:

Session.begintransaction ();//Map entity with database map user = 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 (?,?)


It is said that dynamic mapping is convenient for storing data. But extracting data from the data seems to be more difficult to handle. In fact, the 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 Double sword. We will be able to implement the requirements of our dynamic build table.


In the spring configuration file, add:

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

Use wildcard characters to configure Hbm.xml files. is to be compatible with the dynamic model profile generated during the execution period, and Jrebel can detect changes to the configuration file, thereby loading the new configuration into the file. It is necessary to note that the dynamic loading of Jrebel is lazy loading, that is, when you use the change, the content of your changes will be loaded in again.


Span style= "Font-family:microsoft yahei;font-size:16px;font-size:16px;" > > I'm also the first to use Hibernate dynamic model, and now it's a trial phase. Let's say that everyone who used or interested in dynamic models welcomed the message exchange.



Rookie learn ssh (18) --hibernate Dynamic Model +jrebel implement dynamic creation of tables

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.