Getting Started 13 - MAP Map
Suppose you now want to design an online file management, each user can upload your own file, add a description to the file, we can use the MAP type object to record uploaded files, with the file description as a key (Key), The file name is called value (Value), and our User category is designed as follows:
User.java
Package online.
Import java.util. *;
PUBLIC CLASS User {
Private long id;
PRIVATE STRING NAME;
Private map files = new hashmap ();
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 map getfiles () {
Return Files;
}
Public void setfiles (map files) {
THIS.FILES = Files;
}
Public void addfiles (string des, string name) {
FILES.PUT (DES, NAME);
}
}
To image Map in the database, we use the
User.hbm.xml
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
Property>
map>
clas>
hibernate-maping>
Similarly, we use two tables to image user with the Map object data included in which two tables share the same user_id, assume that we use the following programs to store two USER's data:
User USER1 = New user ();
User1.setname ("CATERPILLAR");
User1.addfiles ("Libary of Hibernate", "Hibernate2.jar");
User1.addfiles ("Libary IF JDBC", "JDBC.jar");
User USER2 = New user ();
User2.setname ("momor");
User2.addfiles ("Cool Fan", "Fan.jpg");
User2.addfiles ("Fat Dog", "Bush.jpg");
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
Session.save (user1);
Session.save (user2);
TX.comMit ();
session.close ();
The table data in the database will be recorded as follows:
MySQL> Select * from user;
-------- -----------
| User_id | NAME |
-------- -----------
| 1 | CATERPILLAR |
| 2 | MoMor |
-------- -----------
2 rows in set (0.01 sec)
Mysql> Select * from files;
--------- -----------------------------------
| User_id | FileName | Description |
--------- -----------------------------------
| 1 | jdbc.jar | Libary IF JDBC |
| 1 | Hibernate2.jar | Libary of Hibernate |
| 2 | fan.jpg | Cool Fan |
| 2 | Bush.jpg | Fat Dog |
--------- -----------------------------------
4 rows in set (0.00 sec)