When developing with Hibernate, it is found that some configuration attributes need to be added when the main table is built, and there is no need to change the main table structure, it is only necessary to build a configuration table, configure the key and value on the configuration table in the model configuration of the main table, for example:
Main Table: CREATE table ' Skill_group ' (
' ID ' int (one) not NULL auto_increment,
' Skill_group ' varchar () not NULL,
' TYPE ' int (one) is not NULL,
' work_end_time ' varchar DEFAULT NULL,
' work_start_time ' varchar DEFAULT NULL,
' sp_id ' varchar DEFAULT NULL,
' user_id ' int (one) DEFAULT NULL,
PRIMARY KEY (' ID '),
UNIQUE KEY ' sp_id ' (' sp_id '),
KEY ' fk508c0ad1aa67812b ' (' sp_id '),
KEY ' fk508c0ad11dd0822b ' (' user_id ')
) Engine=innodb auto_increment=1 DEFAULT Charset=utf8 checksum=1 delay_key_write=1 row_format=dynamic;
Configuration table: CREATE table ' Skill_configuration ' (
' Config_key ' varchar () not NULL,
' config_value ' varchar () not NULL,
' skill_group_id ' int (one) not NULL
) Engine=innodb DEFAULT Charset=utf8;
Configured on the main table in model (Tskillgroup):
Private map<string, string> configmap = new hashmap<string, string> (); Configuration associated with the skill group, stored in map
@ElementCollection (Fetch=fetchtype.eager)
@MapKeyColumn (name = "Config_key")//The KEY value of the configuration property
@Column (name = "Config_value", length = 290)//The value of the configuration property
@CollectionTable (name = "Skill_configuration",//Configure table table name
Joincolumns = @JoinColumn (name = "skill_group_id"))//primary key for the primary table
Public map<string, String> Getconfigmap () {
return configmap;
}
public void Setconfigmap (map<string, string> configmap) {
This.configmap = Configmap;
}
When adding and Tskillgroup objects, synchronize the configuration table properties with the following methods:
Tskillgroup TSG = new Tskillgroup ();
...... Set the object properties in
Add object: Skillgroupdao.save (TSG);
Synchronization configuration attribute table data: Tsg.getconfigmap (). Put (configuration attribute key, config attribute value);
Modified object: Tskillgroup TSG = Skillgroupdao.get (Tskillgroup.class, Skillgroup.getid ());
..... Set the newly modified value to the object
Synchronization configuration attribute table data: Tsg.getconfigmap (). Put (configuration attribute key, configuration property update value);
Delete object: Tskillgroup TSG = Skillgroupdao.get (Tskillgroup.class, sgId);
Delete all configuration properties for this object: Tsg.getconfigmap (). Clear ();
Query object:list<tskillgroup> l = skillgroupdao.find (hql, params);
Loop list Get Tskillgroup object TSG
Isolate the configuration properties for the TSG object: Tsg.getconfigmap (). Get (config attribute key)
Hibernate Correlation Management Configuration table