hibernate中映射list

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。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值