Hibernate学习文档_集合映射


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`)
);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值