package model;
import java.util.ArrayList;
import java.util.List;
public class Team {
private String id;
private String teamName;
private List students = new ArrayList ();
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
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;
}
}
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>
<list name="students" table="student" cascade="all">
<key column = "team_id"></key>
<index column = "index_"></index>
<one-to-many class = "model.Student"/>
</list>
</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>
注意Student类中是没有index_属性的,而在student表中有这个属性。用来记录student表记录的顺序。
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), index_ integer, 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)
* @author Administrator
*
*/
public class Create {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, true);
}
}
package model;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TEST {
public static void main(String[] args) {
save();
}
/**
*
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 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: insert into student (name, card_id, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
*/
@SuppressWarnings("unchecked")
static void save () {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Team team1 = new Team ();
team1.setTeamName("team1");
Team team2 = new Team ();
team2.setTeamName("team2");
Student s1 = new Student ();
Student s2 = new Student ();
Student s3 = new Student ();
Student s4 = new Student ();
Student s5 = new Student ();
Student s6 = new Student ();
s1.setName("zhangsan");
s2.setName("lisi");
s3.setName("wangwu");
s4.setName("zhouliu");
s5.setName("hello");
s6.setName("world");
team1.getStudents().add(s1);
team1.getStudents().add(s2);
team2.getStudents().add(s3);
team2.getStudents().add(s4);
team2.getStudents().add(s5);
team2.getStudents().add(s6);
session.save(team1);
session.save(team2);
tx.commit();
}catch (Exception e) {
e.printStackTrace();
}
}
}
注意不要list标签的inverse改成true,这样就有student来维护,可以student类中是没有index_的属性的。
所以此处必须是inverse=false。