您的位置:首页 > 其它

Hibernate学习文档_集合映射

2011-08-17 21:48 351 查看
Just for backup.

public class CollectionMapping {

private Integer id;
private String name;
private Set<String> set;
private List<String> list;
private Map<String, String> map;
private String[] array;
}


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lohamce.hibernate">

<class name="CollectionMapping" table="t_collection">
<id name="id">
<generator class="native" />
</id>
<property name="name" />

<set name="set" table="t_set">
<key column="setId" />
<element type="string" column="setValue" />
</set>

<list name="list" table="t_list">
<key column="listId"/>
<list-index column="listIndex" />
<element type="string" column="listValue" />
</list>

<array name="array" table="t_array">
<key column="arrId"/>
<list-index column="arrIndex" />
<element type="string" column="arrValue" />
</array>

<map name="map" table="t_map">
<key column="mapId" />
<map-key type="string" column="mapKey" />
<element type="string" column="mapValue" />
</map>

</class>

</hibernate-mapping>


@Test
public void testSaveCollectionMapping(){
Session session = null;
Transaction tx = null;

CollectionMapping c = new CollectionMapping();
c.setName("Collection Mapping nn1");

Set<String> set = new HashSet<String>();
set.add("Set-A");
set.add("Set-B");

List<String> list = new ArrayList<String>();
list.add("List-A");
list.add("List-B");

String[] array = new String[] {"Array-A","Array-B"};

Map<String, String> map = new HashMap<String, String>();

c.setArray(array);
c.setList(list);
c.setMap(map);
c.setSet(set);

try{
session = HibernateUtil.openSession();
tx = session.beginTransaction();

session.save(c);

tx.commit();
} catch (Exception e){
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.close(session);
}
}


CREATE TABLE `t_collection` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

CREATE TABLE `t_array` (
`arrId` INT(11) NOT NULL,
`arrValue` VARCHAR(255) NULL DEFAULT NULL,
`arrIndex` INT(11) NOT NULL,
PRIMARY KEY (`arrId`, `arrIndex`),
INDEX `FK9FFBAECE6AA85C8` (`arrId`),
CONSTRAINT `FK9FFBAECE6AA85C8` FOREIGN KEY (`arrId`) REFERENCES `t_collection` (`id`)
);

CREATE TABLE `t_list` (
`listId` INT(11) NOT NULL,
`listValue` VARCHAR(255) NULL DEFAULT NULL,
`listIndex` INT(11) NOT NULL,
PRIMARY KEY (`listId`, `listIndex`),
INDEX `FKCB5F9189BF6720C5` (`listId`),
CONSTRAINT `FKCB5F9189BF6720C5` FOREIGN KEY (`listId`) REFERENCES `t_collection` (`id`)
);

CREATE TABLE `t_set` (
`setId` INT(11) NOT NULL,
`setValue` VARCHAR(255) NULL DEFAULT NULL,
INDEX `FK68F92177A24B89` (`setId`),
CONSTRAINT `FK68F92177A24B89` FOREIGN KEY (`setId`) REFERENCES `t_collection` (`id`)
);

CREATE TABLE `t_map` (
`mapId` INT(11) NOT NULL,
`mapValue` VARCHAR(255) NULL DEFAULT NULL,
`mapKey` VARCHAR(255) NOT NULL,
PRIMARY KEY (`mapId`, `mapKey`),
INDEX `FK68F7B1174BDE03` (`mapId`),
CONSTRAINT `FK68F7B1174BDE03` FOREIGN KEY (`mapId`) REFERENCES `t_collection` (`id`)
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: