Hibernate Getting Started 11 - SET Mapping

xiaoxiao2021-03-06  43

Getting Started 11 - Set Mapping

This topic introduces how the image of the object and the data table is to include the image of the object and the data table, like SET, which can include all Java objects like SET. The object included in the included object is not an entity (entiy). (Simple, that is, the object has no object recognition (Identity) value, no database hierarchical table and corresponding object, just pure value type (Value Type) object, about Entity Value Type's instructions, you can take a look at the reference manual 5.2.1 or Hibernate In Action Chapter VI.) Suppose We have a User category, in addition to the name properties, the other is the user's email address, the same Users may have multiple different email addresses, so we use the set object in the User category to record, while we use String to record each mail address, in order not to allow repeated email address records, so we use SET object, our User category is as follows:

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 (string addr) {

AddRs.Add (AddR);

}

}

The addaddress () method is to add a one-piece mail address and additionally, we can also set the SET object outside, and then use the setaddrs () method to set it to the User object, on the image file, for SET Image, we use the tag for setting, as shown below:

User.hbm.xml

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

From the mapping file we can see that we use another table AddRS to record the objects that are real recorded in the set, in order to indicate which USER is belonging to each of the data in Addr, we use the foreign key user_id reference User's user_id, the content of the addRs will be the same, and the type of object included in is set, and which field is recorded in. Suppose we use the following programs to save the data of the user:

User USER1 = New user ();

User1.setname ("CATERPILLAR");

User1.addaddress ("Caterpillar@caterpillar.onlyfun.net");

User1.addaddress ("Justin@caterpillar.onlyfun.net");

User1.addaddress ("Justin@fake.com");

User USER2 = New user ();

User2.setname ("momor");

User2.addaddress ("MoMor@caterpillar.onlyfun.net");

User2.addaddress ("momor@fake.com");

Session session = sessionFactory.openSession ();

Transaction tx = session.begintransaction ();

Session.save (user1);

Session.save (user2);

TX.comMit ();

session.close ();

In fact, the contents of the USER and AddRS tables in the database are as follows:

MySQL> Select * from user;

-------- -----------

| User_id | NAME |

-------- -----------

| 1 | CATERPILLAR |

| 2 | MoMor | --------- -------------

2 rows in set (0.00 sec)

mysql> Select * from addrs;

------- -----------------------------------

| User_ID | Address |

------- -----------------------------------

| 1 | Caterpillar@caterpillar.onlyfun.net |

| 1 | Justin@caterpillar.onlyfun.net |

| 1 | Justin@fake.com |

| 2 | MoMor@caterpillar.onlyfun.net |

| 2 | MOMOR@fake.com |

------- -----------------------------------

5 rows in set (0.00 sec)

The following procedure is simple to demonstrate how to remove the data:

Session session = sessionFactory.openSession ();

List users = session.find ("from user");

session.close ();

SessionFactory.Close ();

For (listiterator itrator = users.listiterator (); item.hasnext ();) {

User User = (user) iterator.next ();

System.out.println (user.getname ());

Object [] addrs = user.getaddrs (). Toarray ();

For (int i = 0; i

System.out.println ("/ taddress" (i 1) ":" AddRS [i]);

}

}

转载请注明原文地址:https://www.9cbs.com/read-63543.html

New Post(0)