How Hibernate handles entity classes with variable number of parameters
我想大声告诉你
我想大声告诉你 2017-05-17 10:06:59
0
1
505

Assume that an object Object has several parameters, and the name and number of the parameters are uncertain. I would like to know how the data table should be designed in this case to be more suitable for Hibernate to operate.
I have made one before Solution (I want to hear the masters complain):
A total of two tables are used: one is the tab table and the other is the tabmeta table, and the tid in the tabmeta table is referenced to the tab's id to create a foreign key (in fact, I don't want to create a foreign key in the database) I want to implement this relationship in the program or DAO layer).

Create the corresponding entity class (generated directly through the table using reverse engineering of InteliJ IDEA):

//tab的实体类 @Entity @Table(name = "tab", schema = "springfkhibernate", catalog = "") public class TabEntity { private int id; private String name; private Map tabmetasById; //省略一些该有的函数和geter seter @MapKey(name = "metakey") @OneToMany(mappedBy = "tabByTid") public Map getTabmetasById() { return tabmetasById; } public void setTabmetasById(Map tabmetasById) { this.tabmetasById = tabmetasById; } } //tabmeta的实体类 @Entity @Table(name = "tabmeta", schema = "springfkhibernate", catalog = "") public class TabmetaEntity { private int id; private String metakey; private String metavalue; //省略该有的getter和setter }

Parameters when generating:

But this will produce two entity classes, one is TabEntity and the other is TabValueEntity. The obsessive-compulsive disorder expresses displeasure. Originally, These two tables describe an entity, not a one-to-many relationship between two objects. Now I would like to ask if it is possible to prepare a variable metas of type (Hash)Map in TabEntity, It is specially used to store these uncertain parameters, so that there is only one entity class. But in this case, how should the data be stored back in the database?
If you want to access the entity class generated in this way If one of the variable parameters is used, you need:
TabDao.getEntity(...).getTabmetasById().get('somekey').getMetaValue()
to implement, but if If you understand the data of these two tables as the data of one entity, it should be implemented like this:
TabDao.getEntity(...).getMeta('somekey')
I don’t know if the master has it There is no suggestion in this regard....
Also, I would like to ask the master what impact each parameter will have in the dialog box for relationship creation in IDEA.

I’m quite confused, I hope the gods can give me some advice!

我想大声告诉你
我想大声告诉你

reply all (1)
滿天的星座

It’s time to ask and answer questions....
If you want to implement the method I mentioned above and also implement LazyLoad, IDEA’s automatic generation is basically useless....The generated function does not include the creation of non- If you need to establish a relationship between objects, you can only change the mapping file yourself. First generate the entity class of the tab table, and then add a Map type object to the entity class of the tab (see another page for the specific mapping type). Field data type of a table), generate corresponding getter and seter functions.
Then there is the map file:

    
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!