Hibernate: uso de clases complejas como propiedades

Estoy intentando hacer lo siguiente:

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)

¡Gracias!

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?

Gracias de nuevo!

preguntado el 02 de mayo de 12 a las 19:05

1 Respuestas

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

contestado el 02 de mayo de 12 a las 20:05

But wouldn't this mean that course is a part of Distance? I want to have course be a table on its own too. If that is even possible. - Yonatan K

Yes this is what it means. Course is Part a of Distance. Otherwise you are braking Hibernate rule. Each Entity has its table and its primary Key and if Course has its own Table and supposed to have its Primary Key (@Id) and therefore it supposed to be separate Entity. - danny.lesnik

One small thing: how can I do the same thing, only instead of "course", use something out of the java.util library? Or is the only way is to wrap it in my own object? - Yonatan K

@YonatanK. can you please explain what do you mean by "out of the java.util library", just update your question with example. - danny.lesnik

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.