I'm trying to map a one-to-many / many-to-one relationship in Hibernate with a List of child entities. Doing the same with a Set on other relations works find, but I need to have a List with order of child-entities.
What I've done so far is this:
@Entity
@Table(name="event")
public class Event {
@OneToMany(mappedBy="event", fetch=FetchType.EAGER)
private List<Message> messages; //Message entitiy is the child
//getters+setters+and some other stuff like id
}
And here's the ManyToOne side:
@Entity
@Table(name="message")
public class Message{
@ManyToOne
@JoinColumn(name="eventid")
private Event event;
//getters+setters+and some other stuff like id
}
On this I can insert a Message, it will be added to the database (postgres). But problem is on getting the messages as a List: I get 2 results of the same Message when only one was added and is visible on the database!
Of course, I've done some research and found that an index is needed on a List. But nothing I've tried, worked out. All I got is an exception of: "null index column for collection...". None of the solutions I've found on here and via google seemed to work and there are so many "solutions" that I don't really know which of those are even supposed to work!
Does anyone have an idea on how to realize this relation? I'd be greatfull for any advice!
Thank you!
I have the same problem with an old version of hibernate (3.5.6) and find one good non-invasive workaround: try to change your List<Message>
to Set<Message>
Object and use HashSet
instead of ArrayList
.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With