1

我正在尝试执行以下操作:

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;
}

}  

Course 是我创建的另一个类:

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 和 B 声明为组件,它“成功”但是当我调用session.load(Distance.class,1L)它时返回了正确的对象,但两个课程是空指针。

我该如何定义?!

另外,我怎么能做同样的事情,但是对于库中的复杂类(比如 java.util 中的东西)

谢谢!

更新:我找到了我可以吃蛋糕的方法,并在远程课程上解决它,但有一些对我来说很重要的事情:课程中必须有一个日期对象。我宁愿使用 java.util.Calendar,但如果这是有问题的,还有其他方法可以让我使用一个日期吗?

再次感谢!

4

1 回答 1

1

您可以通过以下方式实现它:

@Entity
@Table(name="distance")
public class Distance {

private Course courseA, courseB;

@Embedded
public getCourseA(){
return this.courseA;
}

@Embedded
public getCourseB(){
return this.courseB;
}

}

现在是 Embeddable 类:

@Embeddable
public class Address implements Serializable{

@Transient
public Long getId() {
    return id;
}

@Column
public String getName() {
    return name;
}

}

@Embaddable类不是实体,所以它不应该有任何主键。这就是为什么你应该@Transient使用 id 属性

于 2012-05-02T19:08:45.677 回答