Jaanus
Jaanus

Reputation: 16541

Hibernate @OneToOne gives error

I have two entities Item.

They are linked with entity Link

Each item can have many links, but link is a connection between two items.

The problem is that the order of items is important. One is always the first item and other is second item.

So my entities are like this:

Item
   private Collection<Link> links = new ArrayList<Link>();
    @OneToOne(cascade = CascadeType.ALL, mappedBy = ????)
    public Collection<Link> getLinks() {
       return links;
   }

Link
private Item firstItem;
private Item secondItem;

@OneToOne(mappedBy = ???)
public Item getFirstItem() {
    return firstItem;
}

@OneToOne(mappedBy = ???)
public Item getSecondItem() {
    return secondItem;
}

What should i put to mappedBy?? Because pseudomapping for Item should me smthin like this: mappedBy = { "firstItem", "secondItem") }

At first I had no mapping and this gives error:

 @OneToOne or @ManyToOne on foo.bar.Item.links references an unknown entity: java.util.Collection

Upvotes: 1

Views: 196

Answers (1)

axtavt
axtavt

Reputation: 242686

You have to create two different collections with mappedBy pointing to firstItem and secondItem, respectively.

If you need to view them as a single collection, do it in code.

Also as already noted you need to use @OneToMany/@ManyToOne instead of @OneToOne. @ManyToOne doesn't need mappedBy.

Upvotes: 3

Related Questions