Hibernate List 매 핑 파일
<?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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.