Hibernate学习的一个问题

jaisokforron 2010-05-06 07:05:50
在学习Hibernate的集合映射(用List映射),想做成多对多的List集合映射,单向的能做成功,可是双向的老是报错, Field 't_index' doesn't have a default value可是一个它要什么默认值啊?代码如下,请指教
public class Student {

private int id;
private String name;
private Date creDate;
private List<Teacher> teachers;……}

public class Teacher {

private int id;
private int age;
private String pro;
private List<Student> students;……}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.vo">
<class name="Teacher" table="t_teacher">
<id name="id">
<generator class="native"/>
</id>
<property name="pro" column="pro"/>
<property name="age" column="age"/>


<list name="students" table="t_teacher_student" cascade="save-update">
<key column="t_id" not-null="true"></key>
<index column="s_index"></index>
<many-to-many class="Student" column="s_id"></many-to-many>
</list>

</class>
</hibernate-mapping>

<hibernate-mapping package="com.test.vo">
<class name="Student" table="t_student">
<id name="id">
<generator class="native"/>
</id>
<property name="name" column="sname"/>
<property name="creDate" column="credate" type="date"/>

<list name="teachers" table="t_teacher_student" cascade="save-update">
<key column="s_id" not-null="true"></key>
<index column="t_index"></index>
<many-to-many class="Teacher" column="t_id"></many-to-many>
</list>

</class>
</hibernate-mapping>

public class TestSave1 extends TestCase {

public void save1(){
Session session=null;
try {
session=HibernateSessionFactory.getSession();
session.beginTransaction();
Student s1=new Student();
s1.setName("s1");
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
try {
s1.setCreDate(sf.parse("1988-08-09"));
} catch (ParseException e) {
e.printStackTrace();
}

Student s2=new Student();
s2.setName("s2");
try {
s2.setCreDate(sf.parse("1988-09-08"));
} catch (ParseException e) {
e.printStackTrace();
}

Teacher t1=new Teacher();

List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
t1.setAge(77);
t1.setPro("computer");
t1.setStudents(list);

session.save(t1);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
} catch(Exception ee){
ee.printStackTrace();
} finally{
HibernateSessionFactory.closeSession();
} }
...全文
184 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jaisokforron 2010-05-10
  • 打赏
  • 举报
回复
我也准备放弃它用Set了,至于业务逻辑,取出来再说,学习了,谢谢……
angel 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangchbao 的回复:]
不建议你使用List来映射···因为List映射需要多对多表中要有一个区分字段··是避免重复的!
建议你使用Set来映射
public class Student {

private int id;
private String name;
private Date creDate;
private Set<Teacher> teachers=new HashSet……
[/Quote]
+1
乱世出英雄 2010-05-09
  • 打赏
  • 举报
回复
用set来映射啊!在set的props标签中配置。
study_liuy 2010-05-08
  • 打赏
  • 举报
回复
唉 怎么用还不会呢!
auklli 2010-05-08
  • 打赏
  • 举报
回复
不能两边都拥有控制权 必须一方放弃维护关系 inverse="true"
hepeng_8 2010-05-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangchbao 的回复:]
不建议你使用List来映射···因为List映射需要多对多表中要有一个区分字段··是避免重复的!
建议你使用Set来映射
public class Student {

private int id;
private String name;
private Date creDate;
private Set<Teacher> teachers=new HashSet……
[/Quote]
tong 一
handsome_huxiulei 2010-05-06
  • 打赏
  • 举报
回复
我也不晓得 没怎么高过..
auklli 2010-05-06
  • 打赏
  • 举报
回复
用list还真不知道怎么搞 用set咯 官方也推荐用。。
huangchbao 2010-05-06
  • 打赏
  • 举报
回复
不建议你使用List来映射···因为List映射需要多对多表中要有一个区分字段··是避免重复的!
建议你使用Set来映射
public class Student {

private int id;
private String name;
private Date creDate;
private Set<Teacher> teachers=new HashSet<Teacher>(0);
}
public class Teacher {

private int id;
private String name;
private Date creDate;
private Set<Student> students=new HashSet<Student>(0);
}

在XML配置文件中使用set节点来映射就哦了!

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧