【问题标题】:What do these JPA annotations mean?这些 JPA 注释是什么意思?
【发布时间】:2015-07-21 06:28:26
【问题描述】:

您能否用简单的“人类”语言解释这些注释的含义。我不想要文档,我只想听到类似 tAttr 表通过主键 InstitID 与 tInstit 表连接,约束如下等。 SQL 语言就可以了 :) 我有点困惑。谢谢

我有这个映射类

    @Entity
    @DiscriminatorValue("Individual")
    @SecondaryTable(schema = "dbo", name="tAttr", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "InstitID")})
    public class FaIndividual extends FaClient 

它扩展了这个:

@Entity
public abstract class FaClient extends FaInstit

及以上扩展

@Entity
@Immutable
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(
    "CASE "
        + "WHEN ((InstType = 0) AND (PropDeal = 0)) THEN 'Individual' "
        + "WHEN ((InstType = 0) AND (PropDeal = 1)) THEN 'Legal' "
        + "WHEN (InstType = 1) THEN 'Bank' "
        + "WHEN (InstType = 2) THEN 'Subdivision' "
    + "END"
)
@Table(schema = "dbo", name = "tInstit")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public abstract class FaInstit

【问题讨论】:

    标签: hibernate jpa hibernate-annotations


    【解决方案1】:

    您的类 FaInstit (@Entity 这意味着这些是持久类,其字段映射到数据库表/列。

    在你的超类 FaInstit 中,你已经指定继承层次结构中的所有实体都将共享同一个主表 - dbo.tInstit - 带有注释 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)

    当对所有实体使用单个表时,您需要将表中的每条记录标识为属于一个或另一个类。这是通过@DiscriminatorColumn 完成的(如果您跳过此注释,将选择默认列名)。可以使用 @DiscriminatorValue 注释指定放置在 DiscriminatorColumn 中的值。因此,每当您坚持 FaIndividual 时,DiscriminatorColumn 中的值将是Individual

    有时我们需要一个实体映射到多个表。您示例中的主表是dbo.tInstit,但我们可以使用@SecondaryTable 注释定义任意数量的辅助表。您已在FaIndividual 中完成此操作,并且您应该在此类的定义(未显示)中看到该类的字段映射到任一表。使用的从表的行由@SecondaryTable属性中的连接信息定义;

    pkJoinColumns = {@PrimaryKeyJoinColumn(name = "InstitID")}
    

    所以我们有一个连接where dbo.tInstit.ID = dbo.tAttr.InstitID,JPA 将根据这个连接信息更新/读取跨两个表的实体字段等。

    你说过

    我不想要文档

    但恐怕这正是你所需要的,因为这里有很多东西要学。我建议挖掘一些涵盖此类基础的教程/示例。

    我希望这能让你继续前进。

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 2014-05-02
      • 2013-05-09
      • 2013-08-20
      • 2014-12-24
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      相关资源
      最近更新 更多