hibernate bag 集合映射

Person.java

package com.aabnn.vo;

import java.util.ArrayList;
import java.util.Collection;

public class Person {
private int id;
private String name;
private Collection<String> schools=new ArrayList<String>();
public Person(){}
public Person(String name,Collection<String> schools){
this.name=name;
this.schools=schools;
}
public int getId() {
return id;
}
@SuppressWarnings(value="unused")
private void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<String> getSchools() {
return schools;
}
public void setSchools(Collection<String> schools) {
this.schools = schools;
}

}


Person.hbm.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">
<hibernate-mapping package="com.aabnn.vo">
<class name="Person" table="person">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="name" column="name" type="string" />
<bag name="schools" table="schools">
<key column="id" />
<element column="name" type="string"/>
</bag>
</class>
</hibernate-mapping>

Test.java

package com.aabnn.test;

import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.aabnn.vo.Person;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
Collection<String> schools=new ArrayList<String>();
schools.add("xiaoxue");
schools.add("zhongxue");
schools.add("daxue");
Person p=new Person("xiaoxiao",schools);
sess.save(p);
tx.commit();
sess.close();
}

}


mysql 数据库

-- Create schema collectionmapping
--

CREATE DATABASE IF NOT EXISTS collectionmapping;
USE collectionmapping;

--
-- Definition of table `person`
--

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(45) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `person`
--

/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` (`id`,`name`) VALUES
(1,'xiaoxiao');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;


--
-- Definition of table `schools`
--

DROP TABLE IF EXISTS `schools`;
CREATE TABLE `schools` (
`id` int(10) unsigned NOT NULL,
`name` varchar(45) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `schools`
--

/*!40000 ALTER TABLE `schools` DISABLE KEYS */;
INSERT INTO `schools` (`id`,`name`) VALUES
(1,'xiaoxue'),
(1,'zhongxue'),
(1,'daxue');
/*!40000 ALTER TABLE `schools` ENABLE KEYS */;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值