JPA:如果孩子有嵌入式ID,如何级联保存实体?

时间:2017-07-31 09:17:56

标签: java hibernate jpa spring-data-jpa

我有以下父母实体。

if(zip_file){
    request.set_response_content_type("application/x-tar-gz");
    request.add_response_header(new http_header_fields::cache_control_no_cache);
    request.send_file(zip_loc.str());
    return true;
}

这是我的孩子实体

@Entity
@Table(name = "vital_entry")
public class VitalEntry {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "vital_entry_id", columnDefinition = "INT")
    private int vitalEntryId;

    @OneToMany(mappedBy = "vitalEntry", cascade = CascadeType.ALL)
    private Set<VitalEntryValues> vitalEntryValues = new HashSet<>();

    .... other fields

}

和EmbeddedId类:

@Entity
@Table(name = "vital_entry_values")
public class VitalEntryValues {

    @EmbeddedId
    private VitalEntryValuesId vevId;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @MapsId("vital_entry_id")
    @JoinColumn(name = "vital_entry_id", columnDefinition = "INT")
    private VitalEntry vitalEntry;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @MapsId("vital_id")
    @JoinColumn(name = "vital_id", columnDefinition = "INT")
    private Vitals vitals;

    @Column(name = "vital_unit_id", insertable = false, updatable = false)
    private int vitalUnitId;

    @Basic
    private double value;

    @Basic
    @Column(columnDefinition = "INT")
    private int type;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinColumn(name = "vital_unit_id", columnDefinition = "INT")
    private VitalUnits vitalUnits;


    public VitalEntryValues() {
    }

    public VitalEntryValues(double value, int type, VitalEntry vitalEntry, VitalUnits vitalUnits, Vitals vitals) {
        this.value = value;
        this.type = type;
        this.vitalEntry = vitalEntry;
        this.vitalUnits = vitalUnits;
        this.vitals = vitals;
    }

}

我将它保存为:

@Embeddable
public class VitalEntryValuesId implements Serializable {

    @Column(name = "vital_entry_id", columnDefinition = "INT", insertable = false, updatable = false)
    private int vitalEntryId;

    @Column(name = "vital_id", columnDefinition = "INT", insertable = false, updatable = false)
    private int vitalId;
}

但是在保存时我得到了这个例外:

VitalEntry = new VitalEntry(accountId, vitalEntryVO.getDateTime(),
                DateUtility.getCurrentGMTDate(), vitalEntryVO.getNotes(), vitalEntryVO.getVitalEntryType(), vitalEntryVO.getParentVitalId());

Set<VitalEntryValues> vitalEntryValues = new HashSet<>();

for (VitalEntryValueVO value : vitalEntryVO.getValues()) {
    VitalEntryValues values = new VitalEntryValues(value.getValue(), vitalEntryVO.getType(), vitalEntry,
    vitalRepo.findUnit(value.getUnitId()), vitalRepo.findOne(value.getVitalId()));
    vitalEntryValues.add(values);
}
vitalEntry.setVitalEntryValues(vitalEntryValues);
vitalEntry =  vitalEntryRepo.save(vitalEntry);

我在这里做错了什么?任何帮助表示赞赏..

修改

Caused by: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.v2.entity.VitalEntryValues
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:804)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:771)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:458)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:383)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:491)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:423)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:386)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:445)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:281)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:765)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
    at com.sun.proxy.$Proxy555.persist(Unknown Source)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:508)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)

1 个答案:

答案 0 :(得分:1)

简介和背景

我使用了提供的源代码的简化版本,足以重现上述问题。

因此,分析和解决方案可能不完整。

Hibernate版本:

Storage::move('uploads/myfile.gif', 'uploads/newfolder/myfile.gif');

分析和解决方案

1。 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> </dependency> 注释用法

请考虑查看MapsId注释及其用法的文档。

对于提供的上下文,它应该是:

MapsId

2。 Hibernate:... public class VitalEntryValues { ... @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) @MapsId("vitalEntryId") @JoinColumn(name = "vital_entry_id", columnDefinition = "INT") private VitalEntry vitalEntry; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) @MapsId("vitalId") @JoinColumn(name = "vital_id", columnDefinition = "INT") private Vitals vitals; ... } 字段初始化

正如我的实验所示,使用EmbeddedId注释注释的字段也需要初始化以避免内部Hibernate异常 - EmbeddedId。有关详细信息,请参阅the question

org.hibernate.PropertyAccessException

希望这有帮助。