Hibernate: Using complex classes as properties

ⅰ亾dé卋堺 提交于 2019-12-13 17:26:34

问题


I am trying to do the following:

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

}  

Where Course is another class I created:

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

}

I tried defining courseA and B as properties of Distance in "distance.hbm.xml" but that just yelled at me with an exception: org.hibernate.MappingException: Could not determine type for: database.datatypes.Course at table:distances...
I have tried declaring courseA and B as components, which "succeeded" but when I called session.load(Distance.class,1L) it returned the right object, but the two courses were null pointers.

How do I define it?!

Also, how can I do the same, but for complex classes from a library (like something out of java.util)

Thanks!

UPDATE: I found the way i could have my cake and work my way around it on the Distance-Course thing, but there is something important for me to work with: Course must have a date object in it. I would rather use java.util.Calendar, but if that is problematic, any other way to have a date that i could use instead?

Thanks again!


回答1:


You can achieve it the following way:

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

private Course courseA, courseB;

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

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

}

Now the Embeddable class:

@Embeddable
public class Address implements Serializable{

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

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

}

@Embaddable class is not entity so it should not have any Primary key. this is why you should but @Transient on id property



来源:https://stackoverflow.com/questions/10419328/hibernate-using-complex-classes-as-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!