1

私はこの状況にいます。Form、FormComponent、FormComponentJoin の 3 つのエンティティがあります。これは、余分な列を持つ多対多の関係です。FormRepository save() メソッドを 1 回呼び出すだけでフォーム エンティティとそのすべての子を保存したいのですが、うまくいきません。これはエラーメッセージです: The error is Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing These are my entities:

@Data
@Entity
@Table(name = "form")
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Form implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    private String name;

    @OneToMany(mappedBy = "form", cascade = { CascadeType.PERSIST, CascadeType.MERGE }, orphanRemoval = true)
    @NotFound(action = NotFoundAction.IGNORE)
    @Singular
    private List<FormComponentJoin> formComponentJoin = new ArrayList<>();

}

@Data
@EqualsAndHashCode
@Entity
@Table(name = "form_component")
@AllArgsConstructor(suppressConstructorProperties = true)
@NoArgsConstructor
public class FormComponent implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @JsonIgnore
    @OneToMany(mappedBy = "formComponent", cascade = CascadeType.ALL, orphanRemoval = true)
    @NotFound(action = NotFoundAction.IGNORE)
    private List<FormComponentJoin> formComponentJoin = new ArrayList<>();

    @ManyToOne
    @JsonIgnore
    private FormComponent parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @Singular
    private List<FormComponent> children;

    private String value;
}

@Entity
@Table(name = "form_component_join")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class FormComponentJoin implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @ManyToOne
    @JoinColumn(name = "form_id", referencedColumnName = "id")
    @NotFound(action = NotFoundAction.IGNORE)
    @JsonIgnore
    private Form form;

    @ManyToOne
    @JoinColumn(name = "form_component_id", referencedColumnName = "id")
    @NotFound(action = NotFoundAction.IGNORE)
    private FormComponent formComponent;

    @Column(name = "position")
    private int pos;

}

@Repository
@Transactional(readOnly = true)
public interface FormRepository extends PagingAndSortingRepository<Form, String> {

}

テストを行うために、CommandLineRunner を使用します。

@Bean
public CommandLineRunner initDatabase(FormRepository formRepository) {
    return (args) -> {

    FormComponent formComponent1 = FormComponent.builder().value("test").build();
    formComponent1.setChildren(null);
    formComponent1.setParent(null);

    FormComponentJoin formComponentJoin1 = FormComponentJoin.builder().pos(1).formComponent(formComponent1).build();

    Form form = Form.builder().name("test").build()).child(formComponentJoin1).build();
    formComponentJoin1.setForm(form);       

    form = formRepository.save(form);   

}

これは完全なスタック トレースです。

原因: org.hibernate.TransientPropertyValueException: オブジェクトが保存されていない一時インスタンスを参照しています - フラッシュする前に一時インスタンスを保存します: org.fao.backend.entity.FormComponentJoin.formComponent -> org.fao.backend.entity.FormComponent at org.hibernate. engine.spi.CascadingActions$8.noCascade(CascadingActions.java:398) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:129) at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java) :442) org.hibernate.event.internal.DefaultPersistEventListener.justCascade(DefaultPersistEventListener.java:188) で org.hibernate.event.internal.DefaultPersistEventListener.entityIsPersistent(DefaultPersistEventListener.java:181) で org.hibernate.event.internal. DefaultPersistEventListener。onPersist(DefaultPersistEventListener.java:145) org.hibernate.internal.SessionImpl.firePersistOnFlush(SessionImpl.java:838) org.hibernate.internal.SessionImpl.persistOnFlush(SessionImpl.java:831) org.hibernate.engine.spi .CascadingActions$8.cascade(CascadingActions.java:357) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293) org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) で org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:379) で org.hibernate.engine.internal.Cascade.cascadeCollection (Cascade.java:319) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:167) org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:158) で org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:91) で(DefaultFlushEventListener.java:55) org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258) で org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) で org.hibernate.engine.transaction。 internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77) .. . 58 共通フレームを省略

4

1 に答える 1