当fk没有指向id时,hibernate不能填充双向关系集合

kkbh8khc  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(107)

我有一个关于hibernate的问题。正如你所看到的,我在EnvelopeEvent和EnvelopeProcess之间有一个多对一的关系,问题是我没有使用ID来做链接,而是在表ENVELOPE_EVENT.ENVELOPE_FK中,它指向ENVELOPE_PROCESS.ENVELOPE_ID,当我插入时,一切都很完美。但是当我尝试选择时,EnvelopeProcess.events总是空的。我打开了hibernate日志,我注意到它使用EnvelopeProcess.id作为查找EnvelopeEvent的键,这不是我的目的。我希望它使用EnvelopeProcess.envelopeId来查找EnvelopeEvent。当我进行调试时,我还注意到org.hibernate.type.CollectionType.foreignKeyPropertyName对于我的类EnvelopeProcess总是null,这也不是应该的。我的Hibernate版本是5.4.27.Final
下面是我的代码:

public class EnvelopeProcess extends AbstractEntity{

private static final long serialVersionUID = 222222L;

private String envelopeId;

private String status;

private String subject;

private Set<EnvelopeEvent> events;

public Long getQuoteId() {
    return quoteId;
}
public void setQuoteId(Long quoteId) {
    this.quoteId = quoteId;
}
public String getEnvelopeId() {
    return envelopeId;
}
public void setEnvelopeId(String envelopeId) {
    this.envelopeId = envelopeId;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getSubject() {
    return subject;
}
public void setSubject(String subject) {
    this.subject = subject;
}
public Set<EnvelopeEvent> getEvents() {
    return events;
}
public void setEvents(Set<EnvelopeEvent> events) {
    this.events = events;
}
//order by trigger time from latest to oldest
public List<EnvelopeEvent> getOrderedEvents(){
    return this.events.stream()
            .sorted((e1, e2) -> e2.getTriggerTime().compareTo(e1.getTriggerTime()))
            .collect(Collectors.toList());
}

public EnvelopeEvent getLatestEvent() {
    return this.getOrderedEvents().get(0);
}

}
<class name="com.pat.storage.entities.EnvelopeProcess" table="ENVELOPE_PROCESS" polymorphism="implicit" mutable="true" dynamic-insert="false" dynamic-update="false" >
    <cache usage="read-write" region="com.pat.storage.entities"/>
    <id name="id" type="long" unsaved-value="null" >
        <column name="ID"/>
        <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
            <param name="sequence_name">ZSEQID_ENVELOPE_PROCESS</param>
            <param name="initial_value">1</param>
            <param name="increment_size">10</param>
            <param name="optimizer">pooled-lo</param>
            <param name="value_column">next_value</param>
            <param name="force_table_use">true</param>
        </generator>
    </id>
    <version name="version" type="int" column="VERSION"/>
    <property name="envelopeId" type="string" length="255">
        <column name="ENVELOPE_ID" not-null="false" unique="false" />
    </property>
    <property name="status" type="string" length="255">
        <column name="STATUS" not-null="false" unique="false" />
    </property>
    <property name="subject" type="string" length="255">
        <column name="SUBJECT" not-null="false" unique="false" />
    </property>
    <set name="events" lazy="false" fetch="select" inverse="true"  cascade="all">
        <cache usage="read-write" region="com.pat.storage.collections"/>
        <key foreign-key="ENVELOPE_EVENT_FKC" not-null="false"   >
            <column name="ENVELOPE_FK"/>
        </key>
        <one-to-many class="com.pat.storage.entities.EnvelopeEvent" not-found="exception"/>
    </set>
    
</class>
public class EnvelopeEvent extends AbstractEntity{

private static final long serialVersionUID = -11111L;
    
private String eventType;

private Date triggerTime;

private EnvelopeProcess envelopeProcess;

public EnvelopeProcess getEnvelopeProcess() {
    return envelopeProcess;
}

public void setEnvelopeProcess(EnvelopeProcess envelopeProcess) {
    this.envelopeProcess = envelopeProcess;
}

public String getEventType() {
    return eventType;
}

public void setEventType(String eventType) {
    this.eventType = eventType;
}

public Date getTriggerTime() {
    return triggerTime;
}

public void setTriggerTime(Date triggerTime) {
    this.triggerTime = triggerTime;
}

}
<class name="com.iqx.storage.entities.EnvelopeEvent" table="ENVELOPE_EVENT" polymorphism="implicit" mutable="true" dynamic-insert="false" dynamic-update="false" >
    <cache usage="read-write" region="com.iqx.storage.entities"/>
    <id name="id" type="long" unsaved-value="null" >
        <column name="ID"/>
        <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
            <param name="sequence_name">ZSEQID_ENVELOPE_EVENT</param>
            <param name="initial_value">1</param>
            <param name="increment_size">10</param>
            <param name="optimizer">pooled-lo</param>
            <param name="value_column">next_value</param>
            <param name="force_table_use">true</param>
        </generator>
    </id>
    <version name="version" type="int" column="VERSION"/>
    <property name="eventType" type="string" length="255">
        <column name="EVENT_TYPE" not-null="true" unique="false"   />
    </property>
    <property name="triggerTime" type="timestamp" precision="3">
        <column name="TRIGGER_TIME" not-null="true" unique="false"/>
    </property>
    <many-to-one name="envelopeProcess" class="com.iqx.storage.entities.EnvelopeProcess"  cascade="none" 
        foreign-key="ENVELOPE_EVENT_FKC" not-null="true" lazy="proxy" fetch="select" property-ref="envelopeId">
        <column name="ENVELOPE_FK" not-null="false"  />
    </many-to-one>
</class>
xxls0lw8

xxls0lw81#

问题解决了,将property-ref添加到集合的键标记是解决方案。

<set name="events" table="ENVELOPE_EVENT" lazy="false" fetch="select" inverse="true" cascade="all">
<key foreign-key="ENVELOPE_EVENT_FKC" not-null="false" property-ref="envelopeId">
    <column name="ENVELOPE_FK"/>
</key>
<one-to-many class="com.pat.storage.entities.EnvelopeEvent"/>

相关问题