Person.java
Person.hbm.xml
Test.java
mysql 数据库
package com.aabnn.vo;
import java.util.HashMap;
import java.util.Map;
public class Person {
private int id;
private String name;
private Map<String,Double> scores=new HashMap<String,Double>();
public Person(){
}
public Person(String name,Map<String,Double> scores){
this.name=name;
this.scores=scores;
}
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 Map<String, Double> getScores() {
return scores;
}
public void setScores(Map<String, Double> scores) {
this.scores = scores;
}
}
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" type="integer" column="id" >
<generator class="identity" />
</id>
<property name="name" type="string" column="name" />
<map name="scores" table="scores">
<key column="id" />
<map-key type="string" column="course" />
<element type="double" column="grade" />
</map>
</class>
</hibernate-mapping>
Test.java
package com.aabnn.test;
import java.util.HashMap;
import java.util.Map;
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();
Map<String,Double> scores=new HashMap<String,Double>();
scores.put("math", 80.00);
scores.put("chinese", 90.00);
scores.put("art", 98.00);
Person p=new Person("xiaoxiao",scores);
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=5 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `person`
--
/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` (`id`,`name`) VALUES
(1,'xiaoxiao'),
(2,'xiaoxiao'),
(3,'xiaoxiao'),
(4,'xiaoxiao');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;
--
-- Definition of table `scores`
--
DROP TABLE IF EXISTS `scores`;
CREATE TABLE `scores` (
`id` int(10) unsigned NOT NULL,
`course` varchar(45) NOT NULL default '',
`grade` double default NULL,
PRIMARY KEY (`id`,`course`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `scores`
--
/*!40000 ALTER TABLE `scores` DISABLE KEYS */;
INSERT INTO `scores` (`id`,`course`,`grade`) VALUES
(1,'art',98),
(1,'chinese',90),
(1,'math',80),
(2,'art',98),
(2,'chinese',90),
(2,'math',80),
(3,'art',98),
(3,'chinese',90),
(3,'math',80),
(4,'art',98),
(4,'chinese',90),
(4,'math',80);
/*!40000 ALTER TABLE `scores` ENABLE KEYS */;