使用连接实体进行Hibernate一对一映射

时间:2018-04-09 14:15:53

标签: java hibernate jpa

一对一提取存在问题。我有一个简单的对象层次结构

package com.example.demojpa;

public class ClaimCreateRequestFake {
    private Long claimCreateRequestId;
    private ChargebackClaimFake ChargebackClaimFake;    
    ... setters and getters are present for each entity
}

public class AbstractClaimFake {
     private Long claimInstanceId;
     private String abstractDescription;
}

public class ChargebackClaimFake extends AbstractClaimFake {
     private Long claimCreateRequestId;
     private String chargebackDescription;
}

Hibernate 5.0.12.Final用作JPA提供程序。我使用带有JPA 2.0的orm.xml映射

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
             version="2.0">

<sequence-generator name="claim_create_request_id_seq"
                    sequence-name="claims.claim_create_request_id_seq"
                    allocation-size="1"
                    initial-value="1"
                    schema="claims"/>

<sequence-generator name="claim_instance_id_seq"
                    sequence-name="claims.claim_instance_id_seq"
                    allocation-size="1"
                    initial-value="1"
                    schema="claims"/>

<entity class="com.example.demojpa.ClaimCreateRequestFake">
    <table name="claim_create_request_fake" schema="claims"/>
    <attributes>
        <id name="claimCreateRequestId">
            <column name="claim_create_request_id"/>
            <generated-value generator="claim_create_request_id_seq" strategy="SEQUENCE"/>
        </id>
        <one-to-one name="chargebackClaimFake" fetch="EAGER">
            <join-column name="claim_create_request_id" referenced-column-name="claim_create_request_id"/>
            <cascade>
                <cascade-all/>
            </cascade>
        </one-to-one>
    </attributes>
</entity>

<entity class="com.example.demojpa.ChargebackClaimFake">
    <table name="chargeback_claim_fake" schema="claims"/>
    <attributes>
        <basic name="claimCreateRequestId">
            <column name="claim_create_request_id"/>
        </basic>
        <basic name="chargebackDescription">
            <column name="chargeback_description"/>
        </basic>
    </attributes>
</entity>

<entity class="com.example.demojpa.AbstractClaimFake">
    <table name="abstract_claim_fake" schema="claims"/>
    <inheritance strategy="JOINED"/>
    <discriminator-column name="dtype"/>
    <attributes>
        <id name="claimInstanceId">
            <column name="claim_instance_id"/>
            <generated-value strategy="SEQUENCE" generator="claim_instance_id_seq"/>
        </id>
        <basic name="abstractDescription">
            <column name="abstract_description"/>
        </basic>
    </attributes>
</entity>
</entity-mappings>

当我尝试持久化ClaimCreateRequestFake对象时,一切正常 - 数据库中为每个描述的表都有一个有效的条目。但是当我尝试使用Spring Jpa Repository方法findOne(Long id)时,问题就出现了。提取的ClaimCreateRequestFake对象具有null作为ChargebackClaimFake引用。我试图跟踪负责ClaimCreateRequestFake提取的SQL查询。查询是:

select claimcreat0_.claim_create_request_id as claim_cr1_2_0_, 
chargeback1_.claim_instance_id as claim_in2_0_1_, 
chargeback1_1_.abstract_description as abstract3_0_1_, 
chargeback1_.chargeback_description as chargeba1_1_1_, 
chargeback1_.claim_create_request_id as claim_cr2_1_1_ 
from claims.claim_create_request_fake claimcreat0_ left outer join 
claims.chargeback_claim_fake chargeback1_ on 
claimcreat0_.claim_create_request_id=chargeback1_.claim_instance_id 
left outer join claims.abstract_claim_fake chargeback1_1_ on 
chargeback1_.claim_instance_id=chargeback1_1_.claim_instance_id 
where claimcreat0_.claim_create_request_id=?

claimcreat0_.claim_create_request_id = chargeback1_.claim_instance_id 用于加入ChargebackClaimFake对象,但JPA状态

join-column name =“claim_create_request_id”referenced-column-name =“claim_create_request_id”

我在使用JPA映射做错了什么?为什么Hibernate使用这样的连接?

0 个答案:

没有答案