Hibernate / JPA - 复合关键子项的一个主键父项

时间:2017-08-05 17:44:19

标签: java hibernate jpa hibernate-mapping

我的jpa映射有什么问题,我试图用一个主键将父类映射到具有复合键的子类,但是它似乎插入了错误的表,它已经生成了2个表但不幸的是我没有绑定外键(policy_value_summary_id)

@Entity
@Table(name = "POLICY_VALUE_SUMMARY")
public class PolicyValueSummary {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="POLICY_VALUE_SUMMARY_ID")
    private Long policyValueSummaryId;

    @MapsId("policyValueSummaryId")
    @OneToMany
    private Set<PolicyValue> policyValues;
}

子类具有复合键,其中一个是父ID。

@Entity
@Table(name = "POLICY_VALUE")
public class PolicyValue {

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "policyValueSummaryId", column = @Column(name = "POLICY_VALUE_SUMMARY_ID")),
            @AttributeOverride(name = "planAtrId", column = @Column(name = "PLAN_ATR_ID")) })
    private PolicyValuePk policyValuePk;

}

这是我的子类复合键。

@Embeddable
public class PolicyValuePk implements Serializable {

    private Long policyValueSummaryId;

    private Long planAtrId;
}

我尝试使用策略值(子类)保存策略汇总值(父级),如下所示

        PolicyValuePk pk = new PolicyValuePk();
        pk.setPlanAtrId(Long.valueOf("1"));

        Set<PolicyValue> policyValues = new HashSet<>();
        policyValues.add(new PolicyValue(pk));

        PolicyValueSummary summary = new PolicyValueSummary();
        summary.setPolicyValues(policyValues);

        repo.save(summary);

这是输出给我的错误

Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into policy_value_summary (policy_value_summary_id) values (?)
Hibernate: insert into policy_value_summary_policy_values (policy_value_summary_policy_value_summary_id, policy_values_plan_atr_id, policy_values_policy_value_summary_id) values (?, ?, ?)
WARN 6880 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 972, SQLState: 42000
ERROR 6880 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-00972: identifier is too long

1 个答案:

答案 0 :(得分:0)

不,在Oracle 12.2版之前,标识符的长度不得超过30个字符。见文件

http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements008.htm#SQLRF00223

但是从版本12.2起,它们最长可达128个字节