我努力做到以下幾點:休眠:使用複雜的類作爲屬性
public class Distance {
private Course courseA, courseB;
private int minDistance;
double cost;
private Long id;
public Distance() {
super();
}
public Distance(Course courseA, Course courseB, int minDistance, double cost) {
super();
this.courseA = courseA;
this.courseB = courseB;
this.minDistance = minDistance;
this.cost = cost;
}
@Override
public String toString() {
return "Distance [courseA=" + courseA + ", courseB=" + courseB
+ ", MinDistance=" + minDistance + ", Cost=" + cost + "]";
}
public Course getCourseA() {
return courseA;
}
public void setCourseA(Course courseA) {
this.courseA = courseA;
}
public Course getCourseB() {
return courseB;
}
public void setCourseB(Course courseB) {
this.courseB = courseB;
}
public int getMinDistance() {
return minDistance;
}
public void setMinDistance(int minDistance) {
this.minDistance = minDistance;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
凡課程是另一個類我創建:
public class Course {
private Long id;
private String name;
private Calendar date;
public Course() {
super();
}
public Course(Long id,String name, Calendar date) {
super();
this.id = id;
this.name = name;
this.date = date;
}
@Override
public String toString() {
return "Course [id=" + id + ", name=" + name + ", examDate=" + date
+ "]";
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Calendar getDate() {
return date;
}
public void setDate(Calendar date) {
this.date = date;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Course)) {
return false;
}
Course other = (Course) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
}
我試圖定義courseA和B作爲「distance.hbm.xml」中距離的屬性,但只是對我大喊一個例外:org.hibernate.MappingException: Could not determine type for: database.datatypes.Course at table:distances...
我曾嘗試聲明courseA a nd B作爲組件,它「成功」,但當我調用session.load(Distance.class,1L)
時,它返回了正確的對象,但兩個過程都是空指針。
我該如何定義它?
另外,我怎麼可以這樣做,但是從圖書館綜合類(比如東西出來java.util中的)
謝謝!
更新: 我找到了方法,我可以在距離課程中繞過它,但是有一些重要的事情要處理:課程必須有一個日期對象。我寧願使用java.util.Calendar,但如果這是有問題的,有任何其他方式有一個日期,我可以使用?
再次感謝!
但是,這不意味着該課程是距離的一部分?我也希望自己有一個表。如果那甚至是可能的。 –
是的,這就是它的意思。課程是距離的一部分。否則,你正在制動休眠規則。每個實體都有它的表和它的主鍵,如果課程有它自己的表並且應該有它的主鍵(@Id)並且因此它應該是單獨的實體。 –
一件小事:我怎麼能做同樣的事情,而不是「當然」,從java.util庫中使用某些東西?或者唯一的方法是把它包裝在我自己的對象中? –