Hibernate 中间表有其他字段Annotation怎么注解

Luger 2012-06-02 11:18:21
如题,假如说我有一个Student类还有一个Course类表示学生和成绩 他们的关系是多对多,如果想在他们的中间表中加一个字段比如score成绩用注解的方式怎么弄呢?
我看了看这篇文章 试了试不行啊http://blog.sina.com.cn/s/blog_638ea2c50100u47y.html
望大家说说自己的看法或者其他解决方案 最好要Annotation的 谢谢大家
...全文
216 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ch_samuel 2014-10-14
  • 打赏
  • 举报
回复
引用 3 楼 LFD619100 的回复:
以学生(T_Student)和课程(T_Course)之间的多对多关系,中间表Score(分数),学生表和课程表是多对多关系,另外为他们的关系添加额外的字段---分数: T_Student类如下: package server.com.upc.test; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.MapKey; @Entity public class T_Student { private int id; private String name; private Map<String,T_Crouse> course=new HashMap<String,T_Crouse>(); /* * 或者 * private Set<T_Crouse> course=new HashSet<T_Crouse>(); * * */ @Id @GeneratedValue 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; } /* * 或者 * @ManyToMany @JoinTable( name="score", joinColumns=@JoinColumn(name="student_id"), inverseJoinColumns=@JoinColumn(name="course_id") ) * */ @ManyToMany @MapKey(name="id") @JoinTable( name="score", joinColumns=@JoinColumn(name="student_id"), inverseJoinColumns=@JoinColumn(name="course_id") ) public Map<String, T_Crouse> getCourse() { return course; } public void setCourse(Map<String, T_Crouse> course) { this.course = course; } } T_course类: package server.com.upc.test; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class T_Crouse { private int id; private String name; @Id @GeneratedValue 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; } } 中间表Score也写成实体类: package server.com.upc.test; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity @Table(name="score") public class T_Score { private int id; private int scrores; private T_Student student; private T_Crouse course; @Id @GeneratedValue public int getId() { return id; } public int getScrores() { return scrores; } public void setScrores(int scrores) { this.scrores = scrores; } public void setId(int id) { this.id = id; } @ManyToOne @JoinColumn(name="student_id") public T_Student getStudent() { return student; } public void setStudent(T_Student student) { this.student = student; } @ManyToOne @JoinColumn(name="course_id") public T_Crouse getCourse() { return course; } public void setCourse(T_Crouse course) { this.course = course; } } 注意的是中间表中的导航关系manytomany @JoinColumn(name="course_id");@JoinColumn(name="course_id")中声明的course_id,student_id和T_student表中声明的要一致,不然会产生其他的字段--再就是中间表的@Table(name="score")score名字和T_Student中的 @JoinTable( name="score",要一样!!! 建立好之后就会生成中间表含有字段id,student_id,course_id,和score四个字段(然后hibernate生成的主键是(student_id,coure_id))虽然你在T_Score表中声明了自己的id。。。。这是值得注意的地方!
说得对
LFD2584 2012-09-08
  • 打赏
  • 举报
回复
以学生(T_Student)和课程(T_Course)之间的多对多关系,中间表Score(分数),学生表和课程表是多对多关系,另外为他们的关系添加额外的字段---分数:

T_Student类如下:

package server.com.upc.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MapKey;

@Entity
public class T_Student {
private int id;
private String name;
private Map<String,T_Crouse> course=new HashMap<String,T_Crouse>();
/*
* 或者
* private Set<T_Crouse> course=new HashSet<T_Crouse>();
*
* */
@Id
@GeneratedValue
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;
}

/*
* 或者
* @ManyToMany
@JoinTable(
name="score",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="course_id")
)
*
*/
@ManyToMany
@MapKey(name="id")
@JoinTable(
name="score",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="course_id")
)
public Map<String, T_Crouse> getCourse() {
return course;
}
public void setCourse(Map<String, T_Crouse> course) {
this.course = course;
}

}



T_course类:

package server.com.upc.test;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class T_Crouse {
private int id;
private String name;

@Id
@GeneratedValue
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;
}

}
中间表Score也写成实体类:

package server.com.upc.test;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity

@Table(name="score")
public class T_Score {
private int id;
private int scrores;
private T_Student student;
private T_Crouse course;

@Id
@GeneratedValue
public int getId() {
return id;
}
public int getScrores() {
return scrores;
}
public void setScrores(int scrores) {
this.scrores = scrores;
}
public void setId(int id) {
this.id = id;
}

@ManyToOne
@JoinColumn(name="student_id")
public T_Student getStudent() {
return student;
}

public void setStudent(T_Student student) {
this.student = student;
}
@ManyToOne
@JoinColumn(name="course_id")
public T_Crouse getCourse() {
return course;
}

public void setCourse(T_Crouse course) {
this.course = course;
}

}



注意的是中间表中的导航关系manytomany @JoinColumn(name="course_id");@JoinColumn(name="course_id")中声明的course_id,student_id和T_student表中声明的要一致,不然会产生其他的字段--再就是中间表的@Table(name="score")score名字和T_Student中的 @JoinTable(
name="score",要一样!!!

建立好之后就会生成中间表含有字段id,student_id,course_id,和score四个字段(然后hibernate生成的主键是(student_id,coure_id))虽然你在T_Score表中声明了自己的id。。。。这是值得注意的地方!
fj359941160 2012-06-10
  • 打赏
  • 举报
回复
两种方式。

第一种:中间表是个类。

类里面有Student和Course两个变量。 都是ManyToOne关系。

第二种:
Student里有个集合是Course。OneToMany关系。

有个生成的数据库有bug的。 具体忘了。 试试就知道了。

参考:
http://blog.csdn.net/fj359941160/article/details/7595946

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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