Are there some more in-depth articles on dynamic model?

There is too little content about this in the user guide (, and I can’t figure out many details, such as how to obtain the data of dynamic entities, how to dynamically update the “hibernate-mapping” , and other points that need attention.
I googled, but still can’t find anything worthwhile.
So, where are some more in-depth articles, or reference projects that use this feature?
Thank you.

By the way, I want to create a program, all of its entities are dynamically created by customers (and stored as metadata, not Java classes). After they create the entity class, they will insert data into it, and add or delete columns. I think the dynamic model of Hibernate is the feature what I want.

For dynamic mappings you have to use hbm.xml like shown in the user guide. Dynamic mappings is a very advanced concept and if you don’t know static mappings well enough, it will be very hard to understand what is going on.

Every entity type has an entity-name. In annotated models, the entity name can be specified through @Entity("..."). This is the name that you can use in HQL to refer to this type.

So if you want to query data from such an entity, you will have to query e.g. select e from MyEntityName e.

When saving/deleting/updating, you will also always have to specify the entity name as additional argument to the respective operations on Session.

I would advise you to not use dynamic mappings as that comes with a lot of manual mapping complexity. Apart from that, how do you want to manage the database schema? I doubt that you want to use the same database user you use for OLTP also for DDL statements.

I would suggest you to use static models as much as possible and model the dynamic parts through a JSON column:

1 Like

Hello beikov, thank you for your explanation and advice.

In my idea, a program instance corresponds to only one schema (database), and in this instance, a customer group (such as a school class) will share this schema. Only specific customers (such as teachers) can create and modify tables. So after a customer causes the DDL operation to be performed, all Hibernate sessions of this instance need to be updated to adapt to the new data structure. (I read some posts and found that every time the data structure changes, the SessionFactory of Hibernate needs to be recreated. I don’t know how much impact this recreation has on the singleton-based Spring architecture.)

Since the needs of each customer group are different, I can’t implement too many static tables. Most tables are created on their demand.

As for the data structure itself (the metadata mentioned above), it can be stored in static tables in the same database, or it can be stored in files on the server (since this part of data is small and does not change frequently).

Useful articles I found so far:
Programmatically adding HBM mapping to the configuration
Dynamically adding entities to hibernate
Domain models and metadata (from section “Creating dynamic applications” to the end)

Well, you can do that, but prepare yourself to suffer. This over generic meta approach is going to perform very poorly and yes, you’ll have to recreate the SessionFactory after every schema change especially also because you have to regenerate the XML mappings.

1 Like