Hi, I'm using:
Hibernate 3.1rc2 with
Annotations: 3.1beta6
MySql 5.0.13-rc, HSQLDB 1.8.0
I have the following problem:
I have a hierarchy of classes, that I want to persist to the database. Everything was going just fine, I managed to get Hibernate persist them the way I wanted.
(source listing follows shortly - I'll explain the problem first)
But then
I wanted to add a collection of entities using the @OneToMany (Unidirectional) association to one point (class) in the hierarchy. I used the following annotation:
Code:
@OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = EventComment.class)
@JoinColumn (name = "event_fk", referencedColumnName = "id")
private Collection<EventComment> comments = new ArrayList<EventComment>(0);
So I tried to persist such an object (a concrete class of course - see below). Everything seemed OK...the cascading worked fine - the collection elements made it to the database...all seemed well except for the little detail, that the
foreign key "event_fk" (Hibernate correctly added the column to the right table) remained NULL. Naturally, this means, that the collection IS persisted for the object, but when Hibernate tries to read it back from the database, the foreign keys are missing and so the returned collection is empty...
I also tried the recommended way - via a JoinTable, but the results were similar - the jointable was created, the collection elements were persisted correctly, but the jointable stayed empty...
here's the source listing for the classes in question (without irrelevant methods & fields of course )Code:
@EmbeddableSuperclass (access = AccessType.FIELD)
public abstract class AbstractEvent implements Serializable {
...
}
@Entity (access = AccessType.FIELD)
@Table (name = "object_events")
@Inheritance (strategy = InheritanceType.JOINED)
public abstract class ObjectEvent extends AbstractEvent {
/** The primary key of this object. */
@Id (generate = GeneratorType.AUTO)
private Long id = null;
...
}
@Entity (access = AccessType.FIELD)
@Table (name = "warning_events")
@Inheritance (strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn (name = "id")
public abstract class WarningEvent extends ObjectEvent {
/** The operator comment(s) for this event. */
@OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = EventComment.class)
@JoinColumn (name = "event_fk", referencedColumnName = "id")
private Collection<EventComment> comments = new ArrayList<EventComment>(0);
...
}
! There are of course concrete classes that extend the abstract ones above...Code:
@Entity (access = AccessType.FIELD)
@Table (name = "timeouts")
@PrimaryKeyJoinColumn (name = "id")
public class TimeoutEvent extends WarningEvent {
...
}
and here's the entity that's contained in the collection Code:
@Entity (access = AccessType.FIELD)
@Table (name = "event_comments")
public class EventComment implements Serializable {
/** primary key */
@Id (generate = GeneratorType.AUTO)
private Long id;
}
I searched the manuals, various forums, googled as hell, but I could't get it work correctly. I think I must've made a silly mistake somewhere and maybe it's plain to see, but I just can't find what's wrong...so any help is really appreciated. Thanx in advance...hope someone reads this :)