1.hibernate.cfg.xml
代码如下 | 复制代码 |
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <property name="hibernate.c3p0.max_statements">100</property> |
2.持久化类 Member.java
代码如下 | 复制代码 |
package org.Rudiment.hibernate; import java.util.HashMap; public class Member { |
3.持久化类的组件类 Address.java
代码如下 | 复制代码 |
package org.Rudiment.hibernate; import java.util.HashMap; public class Address { private String street; private Member owner; |
4.持久化类的映射文件 Member.cfg.xml
代码如下 | 复制代码 |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2013-9-9 19:50:34 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="org.Rudiment.hibernate"> <class name="Member" table="TEST_MEMBER"> <id name="id" column="ID"> <generator class="native"></generator> </id> <property name="age" column="AGE"></property> <property name="name" column="NAME"></property> <map name="addresses" table="test_component"> <key column="component_id" not-null="true" /> <composite-map-key class="Address"> <key-property name="street" column="home_street" type="string" /> <key-property name="zipCode" column="home_zip_code" type="string" /> <key-property name="city" column="home_city" type="string" /> </composite-map-key> <element column="address_name" type="string" /> </map> </class> </hibernate-mapping> |
5.持久化类的操纵类 MemberHandler.java
代码如下 | 复制代码 |
package org.Rudiment.hibernate; import java.util.HashMap; import org.hibernate.Session; public class MemberHandler { public static void insert() { Address ad2 = new Address(); Map<Address, String> am = new HashMap<Address, String>(); m.setAddresses(am);
|
注:
当运行MemberHandler的时候将发现,后台数据库多了一个 test_member 和 test_component 的数据表,表的内容如下:
代码如下 | 复制代码 |
mysql> select * from test_member; +----+------+-------+ | ID | AGE | NAME | +----+------+-------+ | 1 | 24 | Louis | +----+------+-------+ 1 row in set (0.00 sec) mysql> select * from test_component; mysql> desc test_member; mysql> desc test_component; |