Hibernate IndexColumn 中的 0 值映射为 base=1

发布于 2024-08-23 09:36:45 字数 1294 浏览 11 评论 0原文

我们正在针对 Oracle 数据库使用 Hibernate Annotations 3.4.0GA 和 Hibernate Core 3.3.2.GA(也称为当前稳定版本)

我们有一个 base=1 的一对多映射,它在很长一段时间内工作得很好,然而上周我们在数据库中发现了一些条目,其中索引列包含值 0,这导致了各种问题。

所以我的问题是:有人知道当一对多关系的索引列与 base=1 映射时将 0 值放入索引列中的方法吗?可能与使用泛型或 MappedSuperclass 有关。

请注意,代码相当复杂,因为还涉及继承。

以下是类的相关部分:

// 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;
}

实际的类继承自这些类,并为类型参数提供具体的类。它们被映射为实体。它们还指定 id 和 version 列以及大量其他属性和引用,但与手头的映射无关。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2024-08-30 09:36:45

我知道已经过去了快两年了,但我在寻找同一问题的解决方案时偶然发现了这一点。我们通过 xml 文件使用 hbm,所以我不太确定这是否有帮助。
在我们的例子中,问题是反向映射。如果对列表(和索引)的控制位于列表元素侧(“许多”),就像您的情况一样,我们就会遇到问题。将其向上移动解决了这个问题。不知道如何通过注释来完成此操作。

错误的另一个来源可能是您在 AbstractReihung 对象上实际设置了“元素”。 Hibernate 使用代理对象来延迟加载属性。如果您设置一个全新的集合而不是修改现有集合,则会覆盖代理对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文