Getting Started 15 - Collection of Components
The collection image introduced by previously introduced only the String property represents the content of a database field. In the previous topic we have seen, we can put a field of content image to a Component object: Getting started 10 - Component map If we want To store the Component object to a collection object, for example store the Email object to the Hashset, you only need to change the
User.java
Package online.
Import java.util.hashset;
Import java.util.set;
PUBLIC CLASS User {
Private long id;
PRIVATE STRING NAME;
Private set addrs = new hashset ();
Public set getaddrs () {
Return AddRS;
}
Public void setaddrs (set adds) {
THIS.Addrs = AddRS;
}
Public long getId () {
Return ID;
}
Public void setid (long id) {
THIS.ID = ID;
}
Public string getname () {
Return Name;
}
Public void setname (String name) {
THIS.NAME = Name;
}
Public void addaddress (email addr) {
AddRs.Add (AddR);
}
}
Our Email category is as follows:
Email.java
Package online.
PUBLIC CLASS Email {
PRIVATE STRING ADDRESS
Public void setaddress (String address) {
THIS.ADDRESS = Address;
}
Public string getaddress () {
Return Address;
}
Public void sendmail () {
System.out.println ("Send Mail TO:" Address;
}
}
To map these two categories, the mapping file is written as follows:
User.hbm.xml
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
Property>
composite-element>
set>
clas>
hibernate-maping>
You can see that the table field in our database is constant, which is also the two tables of User and AddRS. When the work made by Hibernate is to fill in the data in AddRS according to the image file when stored data or removing data. Email object or take it out. Similarly, you can also use the Map object to store the Component, and
composite-element>
map>