Hibernate:使用复杂的类作为属性

时间:2012-05-02 18:10:35

标签: hibernate properties hbm

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

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

}

我尝试在“distance.hbm.xml”中将courseA和B定义为Distance的属性,但是我只是向我大喊异常: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,但如果这有问题,还有其他方法可以使用我可以使用的日期吗?

再次感谢!

1 个答案:

答案 0 :(得分: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类不是实体,因此它不应该有任何主键。这就是为什么你应该在id属性

@Transient