Hibernate中的OneToOne在调用Mongo save时会导致StackOverflow异常

时间:2018-04-19 08:22:23

标签: spring mongodb hibernate

我有两个实体:

发票:

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "invoices")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice implements Serializable {

    private static final long serialVersionUID = 1L;

    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(columnDefinition = "CHAR(36)")
    @Id
    private String id;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "invoice")
    private InvoiceSequence invoiceSequence;
... // skipped for brevity

InvoiceSequence

@Entity
public class InvoiceSequence {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long seqId;

    @OneToOne
    @JoinColumn(name = "invoice_id", nullable = false)
    @JsonIgnore
    private Invoice invoice;
... // skipped for brevity

调用mongo save时,如:

@Override
    public Invoice save(Invoice invoice) {

        Invoice savedInv = invoiceRepository.save(invoice);
        InvoiceSequence seq = new InvoiceSequence();
        seq.setInvoice(savedInv);
        InvoiceSequence savedSeq = invoiceSequenceRepository.save(seq);
        savedInv.setInvoiceSequence(savedSeq);

        return savedInv;
    }

我得到:

java.lang.StackOverflowError
    at java.lang.Class.isInstance(Native Method)
    at java.lang.Class.cast(Class.java:3368)
    at java.lang.invoke.DirectMethodHandle$Accessor.checkCast(DirectMethodHandle.java:418)
    at java.lang.invoke.DirectMethodHandle.checkCast(DirectMethodHandle.java:487)
    at com.vulog.billing.domain.Invoice_Accessor_5oixbb.getProperty(Unknown Source)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:432)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:425)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:330)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:425)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:527)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:437)

我缺少什么?

感谢您的帮助

0 个答案:

没有答案