English 中文(简体)
特殊类别:使用复杂类别作为财产
原标题:Hibernate: Using complex classes as properties

我努力做到以下几点:

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

iii

如果课程是另一类课程,则设立一类课程:

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

iii

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.

我如何界定它?

此外,我如何能够这样做,但对于图书馆的复杂班级(例如,java.util)。

感谢!

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?

再次感谢!

最佳回答

您可以采取以下方式:

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

private Course courseA, courseB;

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

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

}

现改为:

@Embeddable
public class Address implements Serializable{

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

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

}

班级不是实体,因此不应有任何初级钥匙。 因此,你应当只读@transient

问题回答

暂无回答




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签