hibernate中映射list
来源:互联网 发布:微信恶搞踢出群软件 编辑:程序博客网 时间:2024/06/05 03:46
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。
- hibernate中映射list
- hibernate list映射
- Hibernate List映射文件
- hibernate映射list类型
- Hibernate映射List
- Hibernate映射List
- Hibernate的List映射
- Hibernate的List映射
- Hibernate的List映射
- Hibernate映射List
- hibernate中List一对多映射关系详解
- hibernate中List一对多映射关系详解
- Hibernate入门12 - List 映射
- Hibernate入门12 - List 映射
- Hibernate入门之List 映射
- Hibernate集合映射之List
- 【hibernate】映射集合属性list
- Hibernate集合映射之List
- 遇到的问题记下来
- SensorThread线程
- src与href区别
- 电驴提示“该内容尚未提供权利证明,无法提供下载”之解决办法
- MIC编程(6 )——MIC对等模式配置方法
- hibernate中映射list
- 如何仅通过CSS实现多行文本超长自动省略号
- pat 1055 The World's Richest
- 求助!!!Simple_window.h在线等 ,用的code ::blocks
- Mantis时间显示错误(跟系统时间差8个小时)--解决办法
- C++异常处理(一)
- 【迟早得学】CSS3特效之文字阴影:text
- ADB不能正常使用
- POJ 2676—Sudoku