父级中的 @Id 和基类中的唯一序列的正确 JPA 映射是什么

发布于 2024-07-25 04:26:01 字数 754 浏览 3 评论 0原文

我有一个类层次结构:

abstract DomainObject {
...
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ")
    @SequenceGenerator(name="SEQ",sequenceName="SEQ_DB_NAME")
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
...
}

BaseClass extends DomainObject {
...
   // Fill in blank here where this class's @Id will use a unique sequence generator
   // bonus points for any sort of automatic assignment of generator names that might 
   //prevent me from having to instrument all my domain objects uniquely
...
}

注释:

  • 我并不特别需要基类生成器,因此如果我需要删除它也没有问题。
  • 这是一个 oracle 9i db(如果适用)
  • Hibernate 3.4 JPA
  • Spring 2.5 也可用

谢谢

I have a class hierarchy:

abstract DomainObject {
...
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ")
    @SequenceGenerator(name="SEQ",sequenceName="SEQ_DB_NAME")
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
...
}

BaseClass extends DomainObject {
...
   // Fill in blank here where this class's @Id will use a unique sequence generator
   // bonus points for any sort of automatic assignment of generator names that might 
   //prevent me from having to instrument all my domain objects uniquely
...
}

notes:

  • I do not specifically need a base class generator, so if it behooves me to remove it no problem.
  • This is an oracle 9i db if that is applicable
  • Hibernate 3.4 JPA
  • Spring 2.5 is available as well

Thanks

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

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

发布评论

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

评论(2

鲜血染红嫁衣 2024-08-01 04:26:01

好吧,这就是我最终解决问题的方法:

基类:

@MappedSuperclass
public abstract class DomainObject implements Serializable {
 @Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ")
 @Column(name = "id", updatable = false, nullable = false)
 private Long id;

 .. rest of class
}

后代类:

@Entity
@SequenceGenerator(name="SEQ",sequenceName="SEQ_DB_NAME")
public class BusinessObject extends DomainObject {

 ...

}

Okay here's how I ended up solving the problem:

Base class:

@MappedSuperclass
public abstract class DomainObject implements Serializable {
 @Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ")
 @Column(name = "id", updatable = false, nullable = false)
 private Long id;

 .. rest of class
}

Descendant class:

@Entity
@SequenceGenerator(name="SEQ",sequenceName="SEQ_DB_NAME")
public class BusinessObject extends DomainObject {

 ...

}
帅气称霸 2024-08-01 04:26:01

我建议您对基类使用 JOINED 继承类型。 这会将所有公共字段放入基表中,并将自定义字段放入特定表中。 以下是对此的注释:

@Inheritance(strategy=InheritanceType.JOINED)

一旦完成,您几乎可以使用任何排序选项,因为您的所有 ID 始终位于同一个表中。 如果需要,您可以使用单独的序列,但并非所有数据库供应商都支持它。 我想这不是问题,因为您专门使用 Oracle。

我用过这个,看起来效果很好。

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

I would recommend you use the JOINED inheritance type for the base class. This puts all of the common fields in the base table and customizations in specific tables. Here is the annotation for that:

@Inheritance(strategy=InheritanceType.JOINED)

Once that is done, you can pretty much use any sequencing option as all of your IDs are always on the same table. You can use a separate sequence if you want but it is not supported in all database vendors. I guess that is not an issue since you are using Oracle specifically.

I've used this and it seems to work well.

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