Person.java
Person.hbm.xml
Test.java
mysql 数据库
package com.aabnn.vo;
import java.util.HashSet;
import java.util.Set;
public class Person {
private int id;
private String name;
private Set<String> schools = new HashSet<String>();
public Person(){
}
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 Set<String> getSchools() {
return schools;
}
public void setSchools(Set<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" />
<set name="schools" table="schools" cascade="all">
<key column="schoolid" not-null="true" />
<element type="string" column="name" not-null="true" />
</set>
</class>
</hibernate-mapping>
Test.java
package com.aabnn.test;
import java.util.HashSet;
import java.util.Set;
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();
Set<String> schools=new HashSet<String>();
schools.add("快乐小学");
schools.add("轻松中学");
schools.add("时髦大学");
Person p=new Person();
p.setName("小毛");
p.setSchools(schools);
try{
sess.save(p);
}catch(Exception e){
tx.rollback();
}finally{
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) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `person`
--
/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` (`id`,`name`) VALUES
(1,'小毛'),
(2,'小毛');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;
--
-- Definition of table `schools`
--
DROP TABLE IF EXISTS `schools`;
CREATE TABLE `schools` (
`schoolid` int(10) unsigned NOT NULL,
`name` varchar(45) NOT NULL default '',
PRIMARY KEY (`schoolid`,`name`),
CONSTRAINT `fk_schools_person` FOREIGN KEY (`schoolid`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schools`
--
/*!40000 ALTER TABLE `schools` DISABLE KEYS */;
INSERT INTO `schools` (`schoolid`,`name`) VALUES
(1,'快乐小学'),
(1,'时髦大学'),
(1,'轻松中学'),
(2,'快乐小学'),
(2,'时髦大学'),
(2,'轻松中学');
/*!40000 ALTER TABLE `schools` ENABLE KEYS */;