1:value为基本数据类型
package model;
import java.util.HashMap;
import java.util.Map;
public class Team {
private String id;
private String teamName;
private Map students = new HashMap ();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Map getStudents() {
return students;
}
public void setStudents(Map students) {
this.students = students;
}
}
<?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">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate23</property>
<property name = "connection.username">root</property>
<property name = "connection.password">baother520</property>
<property name = "connection.driver_class">com.mysql.jdbc.Driver</property>
<property name = "dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name = "show_sql">true</property>
<mapping resource="Team.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "model.Team" table = "team" lazy="true">
<id name="id" type = "string">
<generator class="uuid"></generator>
</id>
<property name="teamName">
<column name="teamName" length="20"></column>
<type name="string"></type>
</property>
<map name="students" table = "student">
<key column = "team_id"></key>
<index column="name" type = "string"></index>
<element column="description" type = "string"></element>
</map>
</class>
</hibernate-mapping>
package model;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
*
*
* create table student (team_id varchar(255) not null, description varchar(255), name varchar(255) not null, primary key (team_id, name))
create table team (id varchar(255) not null, teamName varchar(20), primary key (id))
alter table student add index FK8FFE823B24EDBC5F (team_id), add constraint FK8FFE823B24EDBC5F foreign key (team_id) references team (id)
*
*/
public class Create {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, false);
}
}
package model;
import java.util.HashMap;
import java.util.Map;
public class Team {
private String id;
private String teamName;
private Map students = new HashMap ();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Map getStudents() {
return students;
}
public void setStudents(Map students) {
this.students = students;
}
}
package model;
public class Student {
private String id;
private String card_id;
private String name;
private int age;
private Team team;
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCard_id() {
return card_id;
}
public void setCard_id(String cardId) {
card_id = cardId;
}
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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "model.Team" table = "team" lazy="true">
<id name="id" type = "string">
<generator class="uuid"></generator>
</id>
<property name="teamName">
<column name="teamName" length="20"></column>
<type name="string"></type>
</property>
<map name="students" table = "student" cascade="all" inverse="true">
<key column = "team_id"></key>
<index column = "card_id" type = "java.lang.String"><!-- 指定map中的key值--></index>
<one-to-many class = "model.Student"/>
</map>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "model.Student" table = "student" lazy="true">
<id name="id" type = "string">
<generator class="uuid"></generator>
</id>
<property name="name">
<column name="name" length="20"></column>
<type name="string"></type>
</property>
<property name="card_id" column="card_id" type = "string"></property>
<property name="age" column="age" type = "int"></property>
<many-to-one name="team" column = "team_id" class="model.Team"></many-to-one>
</class>
</hibernate-mapping>
package model;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* create table student (id varchar(255) not null, name varchar(20), card_id varchar(255), age integer, team_id varchar(255), primary key (id))
create table team (id varchar(255) not null, teamName varchar(20), primary key (id))
alter table student add index FK8FFE823B24EDBC5F (team_id), add constraint FK8FFE823B24EDBC5F foreign key (team_id) references team (id)
*/
public class Create {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, false);
}
}
package model;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TEST {
public static void main(String[] args) {
save();
}
/**
*
* <map name="students" table = "student" cascade="all" inverse="true">
<key column = "team_id"></key>
<index column = "card_id" type = "java.lang.String"><!-- 指定map中的key值--></index>
<one-to-many class = "model.Student"/>
</map>
* 如果添加了inverse="true",需要使用map.put(student1.getCard_id(), student1);
map.put(student2.getCard_id(), student2);
map.put(student3.getCard_id(), student3);
sql:Hibernate: insert into team (teamName, id) values (?, ?)
Hibernate: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
*/
static void save () {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Team team = new Team ();
team.setTeamName("renjiahui");
Map map = team.getStudents();
Student student1 = new Student ();
student1.setAge(210);
student1.setCard_id("aaa");
student1.setName("zhangsan");
student1.setTeam(team);
Student student2 = new Student ();
student2.setAge(220);
student2.setCard_id("bbb");
student2.setName("lisi");
student2.setTeam(team);
Student student3 = new Student ();
student3.setAge(220);
student3.setCard_id("ccc");
student3.setName("wangwu");
student3.setTeam(team);
map.put(student1.getCard_id(), student1);
map.put(student2.getCard_id(), student2);
map.put(student3.getCard_id(), student3);
session.save(team);
tx.commit();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* <map name="students" table = "student" cascade="all">
<key column = "team_id"></key>
<index column = "card_id" type = "java.lang.String"><!-- 指定map中的key值--></index>
<one-to-many class = "model.Student"/>
</map>
* 没有加inverse="true"
* sql:
* Hibernate: insert into team (teamName, id) values (?, ?)
Hibernate: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: update student set team_id=?, card_id=? where id=?
Hibernate: update student set team_id=?, card_id=? where id=?
Hibernate: update student set team_id=?, card_id=? where id=?
*/
static void save2 () {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Team team = new Team ();
team.setTeamName("renjiahui");
Map map = team.getStudents();
Student student1 = new Student ();
student1.setAge(210);
student1.setCard_id("aaa");
student1.setName("zhangsan");
student1.setTeam(team);
Student student2 = new Student ();
student2.setAge(220);
student2.setCard_id("bbb");
student2.setName("lisi");
student2.setTeam(team);
Student student3 = new Student ();
student3.setAge(220);
student3.setCard_id("ccc");
student3.setName("wangwu");
student3.setTeam(team);
map.put("111", student1);
map.put("222", student2);
map.put("333", student3);
session.save(team);
tx.commit();
}catch (Exception e) {
e.printStackTrace();
}
}
}
save2结果: