Some properties of hibernate's set mapping

1. Set Mapping

  1. Collection Properties

attribute Explain Default value
name Name of mapping class property
access Hibernate's policy to access this property defaults to property
table Associated Target Database Table
chema schema name of the target database table
catalog The catalog name of the target database table
lazy Whether to use delayed loading
subselect Define a subquery
sort Set sort type default unsorted unsorted unsorted optional value is natural natural sort, comparatorClass implements interface class as sort algorithm to avoid concurrent use with order-by
inverse Default value used to identify the passive party in a two-way association is false
mutable Identifies whether the associated object can change the default value to true
cascade An optional value for a cascading policy in a setup operation is that all operations cascade, none operations cascade, save-update updates cascade when updating, deletes when deleting, and deletes all-delete-orphan when the associated object loses its host
order-by Set collation
where Add filter condition
fetch Optional values for setting policies for capturing data are join out-of-join capture, select sequence selection capture
atch-size The default value for the number of data read at a time is 1 when delaying loading

2. General Properties

  1. name Specifies the properties of the set collection to be mapped
  2. table set property to map
  3. Key Specifies the foreign key field of the collection table
  4. ElementSpecifies other fields of the collection table
  5. Type element type, be sure to specify

    1. set common properties

    1. Mapping of List Collection Properties
  6. list-index is a list collection must be specified, specify the name of the sort column, because the list is ordered

    1. Map Mapping
  7. map-key Specifies the key of the map

    1. bag is the same as set
  8. You can map properties that are of type java.util.Collection interface.

  9. Its elements are repeatable, but the order is not saved and no additional indexing support is required

    1. Other
  10. inverse defaults to false, which maintains the association itself and is the master

  11. lazy
    1. Use delayed retrieval when true is querying associated objects
    2. false does not use delayed retrieval when querying associated objects
    3. Ext translation is extremely lazy, true is already lazy, lazy equals true is not transmitted when not in use, I use your time you pass again, extra is lazy, even lazy than lazy equals true.
  12. Fetch SQL statement query method (inner join, subquery)
    1. join: Send SQL query Association objects for pressing left outer joins
    2. SelectIt will send multiple SQLs several times to query your associated objects, and join one sql will do it
    3. Subselect: Send a subquery to query the associated object. When using subselect, you need to use the query interface to test. You cannot use the get method to test because the get can't see the effect
  13. Cascade cascade operation

    1. Sample Code
    public class CollectionTest {
       private Integer id;
       private String name;
       private List<Integer> lists;
       private Set<String> sets;
       private Map<String, String> postcode;
       private Collection collection;
    
       public Collection getCollection() {
           return collection;
       }
    
       public void setCollection(Collection collection) {
           this.collection = collection;
       }
    
       public Integer getId() {
           return id;
       }
    
       public void setId(Integer id) {
           this.id = id;
       }
    
       public String getName() {
           return name;
       }
    
       public void setName(String name) {
           this.name = name;
       }
    
       public List<Integer> getLists() {
           return lists;
       }
    
       public void setLists(List<Integer> lists) {
           this.lists = lists;
       }
    
       public Set<String> getSets() {
           return sets;
       }
    
       public void setSets(Set<String> sets) {
           this.sets = sets;
       }
    
       public Map<String, String> getPostcode() {
           return postcode;
       }
    
       public void setPostcode(Map<String, String> postcode) {
           this.postcode = postcode;
       }
    }
    
    1. hbm mapping
    <?xml version="1.0" encoding="utf-8"?>  
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
    
    <hibernate-mapping>   
    
        <class name="CollectionTest" table="TB_COLLECTION">
           <id name="id" column="ID" length="4">
               <generator class="native"/>
           </id>
           <property name="name" column="NAME" length="20"/>
           <set name="sets" table="TB_ADDRESS">
               <key column="CID"/>
               <element column="ADDRESS" type="java.lang.String"/>
           </set>
           <list name="lists" table="TB_PHONE">
               <key column="CID"></key>
               <list-index column="idx"/>
               <element column="PHONE" type="java.lang.Integer"/>
           </list>
           <map name="postcode" table="TB_POST">
               <key column="CID"/>
               <map-key type="string" column="SHORT_NAME"/>
               <element type="string" column="ADDRESS"/>
           </map>
           <!--Bag Type Mapping-->  
           <bag name="bagValue" table="message">  
              <key column="id" />     //Define foreign keys  
              <element type="string" column="bagValue" />  //Define element type  
          </bag>  
       </class>  
    </hibernate-mapping>

Using problem sets

  1. Composite-elementcorresponding instance class equal s and hashcode methods

    The entity class corresponding to the composite-element must override the equal s and hashcode methods, or it will be stored as a set element each time as a new object, resulting in a delete operation on the collection table each time a get is made, followed by an insert operation.

  2. Use of list/array base

    The start subscript of list/array is 0. It is important to note that if the start position of Index in the set table is not 0, it will cause problems with NULL elements in the list/array. At this time, we can use base to synchronize the subscript of Index in the set table, or the subscript of Index in the set table must start with 0.

  3. A little difference from relational mapping

    When using set mapping, we find that the use of set/list and other attributes in the relationship mapping is similar. The one-way one-to-many situation in the relationship mapping is the same as that in the set mapping. The only difference is that the set table does not need a configuration file when using the set mapping, but the one-way one-to-many side must have a configuration file when using the relationship mapping.The biggest difference in a multirelational mapping.

Keywords: Hibernate Database Java SQL

Added by Jnerocorp on Tue, 11 Jun 2019 20:43:24 +0300