Hibernate List 매 핑 파일

4785 단어
Team.hbm.xml
<?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="com.wansha.pojo.Team" table="team">
		<id name="id" column="id" type="int">
			<generator class="identity"></generator>
		</id>
		<property name="teamName" column="teamName" type="string" />
		<list name="student" table="student" cascade="all">
			<key column="team_id"/>
			<index column="`index`"/>
			<one-to-many class="com.wansha.pojo.Student"/>
		</list>
	</class>
</hibernate-mapping>  

Student.hbm.xml
<?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="com.wansha.pojo.Student" table="student">
		<id name="id" column="id" type="int">
			<generator class="identity"></generator>
		</id>
		<property name="name" column="name" type="string" />
		<property name="cardid" column="cardid" type="int" />
		<many-to-one name="team" class="com.wansha.pojo.Team" column="team_id"/>
	</class>
</hibernate-mapping>  

Test.java
package com.wansha.util;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wansha.pojo.Student;
import com.wansha.pojo.Team;

public class Test {
	public static void main(String[] args) {
/*		Team team = new Team();
		team.setTeamName("team 1");
		
		Student student1 = new Student();
		student1.setCardid(111);
		student1.setName("sharp");
		student1.setTeam(team);
		
		Student student2 = new Student();
		student2.setCardid(222);
		student2.setName("hailie");
		student2.setTeam(team);
		
		Student student3 = new Student();
		student3.setCardid(333);
		student3.setName("dudu");
		student3.setTeam(team);
		
		team.getStudent().add(student1);
		team.getStudent().add(student2);
		team.getStudent().add(student3);
		
		Session session = HibernateUtil.getSession();
		Transaction ts = null;
		try{
			ts = session.beginTransaction();
			session.save(team);
			ts.commit();
		}catch(Exception ex){
			if(ts != null){
				ts.rollback();
			}
		}finally{
			HibernateUtil.close(session);
		}*/
		
		//--------------------------------------------------------------------------------
		
		Session session = HibernateUtil.getSession();
		Transaction ts = null;
		Iterator<Student> iter = null;
		try{
			ts = session.beginTransaction();
			Team team = (Team)session.get(Team.class,1);
			List<Student> list = team.getStudent();
			iter = list.iterator();
			ts.commit();
		}catch(Exception ex){
			if(ts != null){
				ts.rollback();
			}
		}finally{
			HibernateUtil.close(session);
		}
		while(iter.hasNext()){
			System.out.println(((Student)iter.next()).getName());
		}
	}
}

Team.java
package com.wansha.pojo;
import java.util.ArrayList;
import java.util.List;

public class Team {
	private int id;
	private String teamName;
	private List<Student> student = new ArrayList<Student>();
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTeamName() {
		return teamName;
	}
	public void setTeamName(String teamName) {
		this.teamName = teamName;
	}
	public List<Student> getStudent() {
		return student;
	}
	public void setStudent(List<Student> student) {
		this.student = student;
	}
}

Student.java
package com.wansha.pojo;

public class Student {
	private int id;
	private String name;
	private int cardid;
	private Team team;
	
	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 getCardid() {
		return cardid;
	}
	public void setCardid(int cardid) {
		this.cardid = cardid;
	}
	public Team getTeam() {
		return team;
	}
	public void setTeam(Team team) {
		this.team = team;
	}
}

좋은 웹페이지 즐겨찾기