deamon
deamon

Reputation: 92377

Distinguish owning and inverse side of a JPA relationship without "mappedBy"

I have a problem with JPA/Hibernate and the mapping of @OneToMany together with @OrderColumn. And I found out that this is probably a problem of a missing distinction between the "owning" and the "inverse" side of the relationship.

But Hibernate does not support the following combination

@OneToMany(mappedBy="...")
@OrderColumn(...)

How can I tell JPA/Hibernate which side the owning side is?

I tried it with

@OneToMany
@OrderColumn(...)
@JoinTable(name="...")

but it didn't help.

It seems to be a Hibernate bug (tried Hibernate 3.6.1 and 3.6.9). Is the combination of mappedBy and @OrderColumn supported by another JPA provider like EclipseLink?

Upvotes: 2

Views: 993

Answers (2)

deamon
deamon

Reputation: 92377

In the end I've found the workaround to put a position into the class (Task) whose objects should be ordered.

@Entity
public class TaskList extends GenericModel {

  @OneToMany(mappedBy="taskList", cascade=CascadeType.ALL, orphanRemoval=true)
  @OrderBy("position")
  public List<Task> tasks = new ArrayList<>();

  public void addTask(int position, task) {
    task.taskList = this;
    tasks.add(position, task);
    updatePositions();
  }

  private void updatePositions() {
    int position = 0;
    for(Whish whish : whishes)
      whish.setPosition(position++);
  }
}

Maybe other JPA providers do a better job in this case, but I have to stick with Hibernate for now.

Upvotes: 0

DataNucleus
DataNucleus

Reputation: 15577

In response to the change of question, such a combination is supported by DataNucleus JPA, as per http://www.datanucleus.org/products/accessplatform_3_0/jpa/orm/one_to_many_list.html#join_bi just replace the @OrderId in that example by @OrderColumn

Upvotes: 2

Related Questions