A specific example shows how to use set ing in Hibernate. For example, a user table is required to record the basic information of a user on a shopping website. This table includes the user ID, name, and mailing address, however, some users may have multiple mailing addresses. In this case, we need to use another form for exclusive storage in the database.
A specific example shows how to use set ing in Hibernate. For example, a user table is required to record the basic information of a user on a shopping website. This table includes the user ID, name, and mailing address, however, some users may have multiple mailing addresses. In this case, we need to use another form for exclusive storage in the database.
Set ing
A specific example shows how to use set ing in Hibernate.
For example, a user table is required to record the basic information of a user on a shopping website. This table includes the user ID, name, and mailing address, however, some users may have multiple mailing addresses. In this case, we need to use another table to separately Store Users' mailing information in the database. In this table, UserId is used as a foreign key of the User table, to associate two tables.
Here we use the automatic table creation mechanism of Hibernate to create corresponding data tables in the database.
To implement automatic table creation, add the following attributes to the Hibernate. cfg. xml file:
update
First, create a user class. The user class has the id, name, and shipping address attributes. Because there are multiple shipping addresses, the String type cannot be used. Therefore, use the collection type, therefore, create the following classes:
Package entity; import java. util. HashSet; import java. util. Set; public class TUser {private int id; private String name; private Set
UserAddresses = new HashSet
(); // Omit the get/set Method}
Then write the specific ing configuration file of the TUser class.
In Hibernate, you need to know the information to access the collection table through the ing file in the user table.
1. Name of the set table (table name)
2. Foreign keys in the set table (set foreign keys)
3. Element columns (set elements) in the collection table)
See the specific configuration code.
A tag is used to specify the Set ing. Its name attribute is used to specify the Set type data attribute name in the POJO class, and table is used to specify the associated data table.
Label is used to specify the foreign key of the associated table. Specify other attributes in the joined table.
Next let's take a look at the test code.
Package Test; import java. util. hashSet; import java. util. set; import org. hibernate. query; import org. hibernate. session; import org. hibernate. transaction; import entity. TUser; import SessionFactory. hibernateSessionFactory; public class Test {private Session session = null; private Transaction tran = null; @ org. junit. testpublic void test () {Set
Set = new HashSet
(); Set. add ("address 1"); set. add ("address 2"); session = HibernateSessionFactory. getSession (); tran = session. beginTransaction (); try {TUser user = new TUser (); user. setId (2); user. setName ("Zhang San"); user. setUserAddresses (set); session. save (user); tran. commit ();} catch (Exception e) {tran. rollback ();} finally {HibernateSessionFactory. closeSession () ;}@ org. junit. testpublic void get () {session = HibernateSessionFactory. getSession (); String hql = "FROM TUser t where t. id = 1 "; Query query = session. createQuery (hql); TUser user = (TUser) query. uniqueResult (); System. out. println (user. getName ());}}
Then, you can query the data in the database.