Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: null index column for collection in one-to-many relation

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!

like image 863
roboneko42 Avatar asked Mar 24 '23 08:03

roboneko42


1 Answers

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!

like image 85
Agustin Silva Albistur Avatar answered Apr 05 '23 21:04

Agustin Silva Albistur