映射集合属性之⑤:Map集合属性
来源:互联网 发布:淘宝天猫历史价格 编辑:程序博客网 时间:2024/06/06 01:00
hibernate.cfg.xml :
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost:3306/temp</property> <property name="connection.username">root</property> <property name="connection.password">mysqladmin</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="db/mapping/Person.hbm.xml"/> </session-factory></hibernate-configuration>Person.java :
public class Person {private int id;private String name;private int age;private Map<String,Integer> scores=new HashMap<String,Integer>();public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Map<String, Integer> getScores() {return scores;}public void setScores(Map<String, Integer> 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="db.domain"> <class name="Person" table="persons"> <id name="id"> <generator class="identity"></generator> </id> <property name="name" type="string"/> <property name="age" type="integer"/> <!-- 映射Map集合属性 --> <map name="scores" table="scores"> <key column="person_id" not-null="true"/> <map-key column="subject" type="string"/> <element column="grade" type="integer"/> </map> </class></hibernate-mapping>Test.java :
public class Test {public static void main(String[] args) {Session session=HibernateSessionFactory.getSession();Transaction txt=session.beginTransaction();Person p1=new Person();p1.setName("小红");p1.setAge(15);Map<String,Integer> scores1=new HashMap<String,Integer>();scores1.put("语文",92);scores1.put("数学",85);p1.setScores(scores1);Person p2=new Person();p2.setName("小明");p2.setAge(14);Map<String,Integer> scores2=new HashMap<String,Integer>();scores2.put("语文",90);scores2.put("数学",96);p2.setScores(scores2);session.save(p1);session.save(p2);txt.commit();HibernateSessionFactory.closeSession();}}
- 映射集合属性之⑤:Map集合属性
- hibernate4 映射集合属性Map
- 5.6.4 映射集合属性:Map集合属性
- 映射集合属性之②:数组属性
- 映射集合属性之①:List集合属性
- 映射集合属性之③:Set集合属性
- hibernate映射集合属性
- Hibernate集合属性映射
- Hibernate映射集合属性
- (五)Hibernate之集合属性映射
- Hibernate映射集合属性2__List、Map、数组、Bag等
- 映射组件属性之②:组件属性为集合
- 映射集合属性之④:bag元素映射
- Hibernate中映射集合属性
- 【hibernate】映射集合属性list
- hibernate4 映射集合属性List
- hibernate4 集合属性映射Array
- hibernate4 映射集合属性 set
- 【ACMclub周赛5】Problem E——TSP旅行商问题
- POJ 1459 Power Network
- [3] Android的init进程
- MU Puzzle(推理找规律)(多校第六场))
- seo培训常识
- 映射集合属性之⑤:Map集合属性
- 确定网站盈利模式方向及可行性分析
- 如何优化MySQL insert性能
- SEO中的精髓
- SQL Server 2012 Express LocalDB 解决provider: SQL Network Interfaces error: 52
- 自己动手写类似酷狗播放器(4)_鼠标右键菜单的添加
- Linux /bin, /sbin, /usr/bin, /usr/sbin 区别
- RTSP传输协议之Methods总结
- 教你如何学习Linux驱动开发