Question

We are using Hibernate Annotations 3.4.0GA and Hibernate Core 3.3.2.GA (also known as the current stable versions) against an Oracle database

We have a One-to-Many mapping with base=1 which worked fine for a loooong time, yet last week we found some entries in the database where the index column contained a value of 0 which caused all kinds of problems.

So my question is: Does anybody know of a way to get a value of 0 into the index column of a one-to-many relationship, when it is mapped with a base=1? Possibly related to the use generics or MappedSuperclass.

Note that code is rather complex, because inheritance is involved as well.

The following are the relevant pieces of the classes:

// SuperClass of the One side
@MappedSuperclass
public abstract class AbstractReihung<Tp, Tw, Te extends AbstractReihungElement<Tp, Tw>>
{
  @OneToMany(cascade = CascadeType.ALL)
  @Cascade(
  {
      org.hibernate.annotations.CascadeType.ALL,
      org.hibernate.annotations.CascadeType.DELETE_ORPHAN
  })
  @JoinColumn(name = "parent_id", nullable = false)
  @IndexColumn(name = "position", base = 1, nullable = false)
  private List<Te> elements = new ArrayList<Te>();
}

// Super Class of the Many side
@MappedSuperclass
public abstract class AbstractReihungElement<Tp, Tw> extends AbstractDbObject
{
  @ManyToOne
  @JoinColumn(name = "parent_id", insertable = false, updatable = false, nullable = false)
  private Tp parent;

  @Column(name = "position", insertable = false, updatable = false, nullable = false)
  private int position;
}

The actual classes inherit from these and provide concrete classes for the type parameters. They are mapped as Entity. They also specify id and version columns as well as tons of other attributes and references, but nothing related to the mapping at hand.

Was it helpful?

Solution

I know it's been almost two years but I stumbled upon this while searching a solution for the same problem. We are using hbm via xml files so I'm not quite sure if this will help. In our case the problem was the reverse mapping. If the control over the list (and index) was on the list-element side (the "many") like in your case we had the problem. Moving it up resolved this. Don't know how this is done with annotations.

Another source for the error can be if you acctually set "elements" on your AbstractReihung object. Hibernate uses proxy objects for lazy loaded properties. If you set a whole new collection instead of modifing the existing one you overwrite the proxy object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top