Hibernate注解@SecondaryTable用途

@SecondaryTable主要用在主表提取成公共表,子表是自行定义,但主键仍然关联的情况,最终hibernate映射时使用两个类(集成关系),但为一个实体,保存到两个表的情况

@SecondaryTable(name = "childtable", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "childid")})
  • childtable标识子表的名称
  • pkJoinColumns 使用join管理的字段是主键,主键名称为childid

SecondaryTable类源码的注释的案例如下:


 * <pre>
 *    Example 1: Single secondary table with a single primary key column.
 *
 *    &#064;Entity
 *    &#064;Table(name="CUSTOMER")
 *    &#064;SecondaryTable(name="CUST_DETAIL",
 *        pkJoinColumns=&#064;PrimaryKeyJoinColumn(name="CUST_ID"))
 *    public class Customer { ... }
 *
 *
 *    Example 2: Single secondary table with multiple primary key columns.
 *
 *    &#064;Entity
 *    &#064;Table(name="CUSTOMER")
 *    &#064;SecondaryTable(name="CUST_DETAIL",
 *        pkJoinColumns={
 *            &#064;PrimaryKeyJoinColumn(name="CUST_ID"),
 *            &#064;PrimaryKeyJoinColumn(name="CUST_TYPE")})
 *    public class Customer { ... }
 * </pre>