Student hypothetical field : s_id, s_name
Course assumptions fields : c_id, c_name
middle table s_c assumptions fields : id, s_id, c_id
then hibernate in the middle of the table needs to be written this entity class do ?
------ Solution ------------------------------------ --------
write , do not write
------ Solution ------------------------ --------------------
No, just add the line corresponding annotation or xml configuration, if the table is automatically created by hibernate in the database to generate the corresponding an intermediate table
------ Solution ------------------------------------ --------
Each course can only be called when there is a student -to-many .
configured to -many do not need .
------ Solution ---------------------------------------- ----
do not remember the details , not long , but you do not see it on oneToMany like, you go to see hibernate many, many of these comments is how to use it
------ For reference only ---------------------------------------
Each course can only be called when there is a student -to-many .
configured to -many do not need .
is not written so that you can
Student category :
@Entity
@Table(name = "student", catalog = "db")
public class Student implements java.io.Serializable {
private Integer id;
private String name;
private String password;
private Boolean isDelete;
private Set<Course> Courses= new HashSet<Course>(0);
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "password")
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "is_delete")
public Boolean getIsDelete() {
return this.isDelete;
}
public void setIsDelete(Boolean isDelete) {
this.isDelete = isDelete;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
public Set<Course> getCourses() {
return this.Courses;
}
public void setTCourses(Set<Course> Coursea) {
this.Courses= Courses;
}
}
Course categories:
@Entity
@Table(name = "course", catalog = "db")
public class Course implements java.io.Serializable {
private Integer id;
private String name;
private Boolean isDelete;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "is_delete")
public Boolean getIsDelete() {
return this.isDelete;
}
public void setIsDelete(Boolean isDelete) {
this.isDelete = isDelete;
}
}
I write it?
------ For reference only -------------------------------------- -
not write , it should be how to deal with?
------ For reference only -------------------------------------- -
wrong.
------ For reference only -------------------------------------- -
not , he will be generated automatically , try not to write, write bad trouble
------ For reference only --------------- ------------------------
I'm a good database design , and reflection is generated .
However , Hibernate automatically generates entity classes will be generated in the middle of the table , so I would like to ask , this middle of the table , it should be how to write ? Need to manually modify it?
------ For reference only -------------------------------------- -
wrong.
can give pointers about it?
------ For reference only -------------------------------------- -
look anyway you like hibernate support
not write that many-
will write two one-
------ For reference only --------------------------- ------------
package com.hibernate;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT", schema = "TEST")
public class Student implements java.io.Serializable {
// Fields
private String stid;
private String stname;
private Set<StudentCourse> studentCourses = new HashSet<StudentCourse>(0);
@Id
@Column(name = "STID", unique = true, nullable = false, length = 40)
public String getStid() {
return this.stid;
}
public void setStid(String stid) {
this.stid = stid;
}
@Column(name = "STNAME", length = 40)
public String getStname() {
return this.stname;
}
public void setStname(String stname) {
this.stname = stname;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "student")
public Set<StudentCourse> getStudentCourses() {
return this.studentCourses;
}
public void setStudentCourses(Set<StudentCourse> studentCourses) {
this.studentCourses = studentCourses;
}
}
package com.hibernate;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "COURSE", schema = "TEST")
public class Course implements java.io.Serializable {
// Fields
private String coid;
private String coname;
private Set<StudentCourse> studentCourses = new HashSet<StudentCourse>(0);
@Id
@Column(name = "COID", unique = true, nullable = false, length = 40)
public String getCoid() {
return this.coid;
}
public void setCoid(String coid) {
this.coid = coid;
}
@Column(name = "CONAME", length = 40)
public String getConame() {
return this.coname;
}
public void setConame(String coname) {
this.coname = coname;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "course")
public Set<StudentCourse> getStudentCourses() {
return this.studentCourses;
}
public void setStudentCourses(Set<StudentCourse> studentCourses) {
this.studentCourses = studentCourses;
}
}
package com.hibernate;
import java.math.BigDecimal;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT_COURSE", schema = "TEST")
public class StudentCourse implements java.io.Serializable {
// Fields
private StudentCourseId id;
private Course course;
private Student student;
private BigDecimal score;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "stid", column = @Column(name = "STID", nullable = false, length = 40)),
@AttributeOverride(name = "coid", column = @Column(name = "COID", nullable = false, length = 40)) })
public StudentCourseId getId() {
return this.id;
}
public void setId(StudentCourseId id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COID", nullable = false, insertable = false, updatable = false)
public Course getCourse() {
return this.course;
}
public void setCourse(Course course) {
this.course = course;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STID", nullable = false, insertable = false, updatable = false)
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
@Column(name = "SCORE", precision = 22, scale = 0)
public BigDecimal getScore() {
return this.score;
}
public void setScore(BigDecimal score) {
this.score = score;
}
}
package com.hibernate;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class StudentCourseId implements java.io.Serializable {
// Fields
private String stid;
private String coid;
public StudentCourseId() {
}
/** full constructor */
public StudentCourseId(String stid, String coid) {
this.stid = stid;
this.coid = coid;
}
@Column(name = "STID", nullable = false, length = 40)
public String getStid() {
return this.stid;
}
public void setStid(String stid) {
this.stid = stid;
}
@Column(name = "COID", nullable = false, length = 40)
public String getCoid() {
return this.coid;
}
public void setCoid(String coid) {
this.coid = coid;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof StudentCourseId))
return false;
StudentCourseId castOther = (StudentCourseId) other;
return ((this.getStid() == castOther.getStid()) || (this.getStid() != null
&& castOther.getStid() != null && this.getStid().equals(
castOther.getStid())))
&& ((this.getCoid() == castOther.getCoid()) || (this.getCoid() != null
&& castOther.getCoid() != null && this.getCoid()
.equals(castOther.getCoid())));
}
public int hashCode() {
int result = 17;
result = 37 * result
+ (getStid() == null ? 0 : this.getStid().hashCode());
result = 37 * result
+ (getCoid() == null ? 0 : this.getCoid().hashCode());
return result;
}
}
------ For reference only ----------------------------------- ----
doing is to the middle of the table is also the entity class , not the middle of the table there is no way to do entity classes ? Because the current scores in the middle of the table and no other attributes , just used the mapping .
------ For reference only -------------------------------------- - many
between them is actually a many to many , LZ you think it
------ For reference only ----------------- ----------------------
resolved
http://blog.csdn.net/xiaobiaobiao521/article/details/9123303
没有评论:
发表评论