1. Set Mapping
-
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
- name Specifies the properties of the set collection to be mapped
- table set property to map
- Key Specifies the foreign key field of the collection table
- ElementSpecifies other fields of the collection table
-
Type element type, be sure to specify
- set common properties
- Mapping of List Collection Properties
-
list-index is a list collection must be specified, specify the name of the sort column, because the list is ordered
- Map Mapping
-
map-key Specifies the key of the map
- bag is the same as set
You can map properties that are of type java.util.Collection interface.
-
Its elements are repeatable, but the order is not saved and no additional indexing support is required
- Other
inverse defaults to false, which maintains the association itself and is the master
- lazy
- Use delayed retrieval when true is querying associated objects
- false does not use delayed retrieval when querying associated objects
- 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.
- Fetch SQL statement query method (inner join, subquery)
- join: Send SQL query Association objects for pressing left outer joins
- SelectIt will send multiple SQLs several times to query your associated objects, and join one sql will do it
- 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
-
Cascade cascade operation
- 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; } }
- 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
-
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.
-
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.
-
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.