Исключение при сохранении списка объектов со своим потомком, временный экземпляр должен быть сохранен перед текущей операцией - PullRequest
0 голосов
/ 21 января 2020

IrTbNotificationHdr & IrTbNotificationDtl два дает право родителю и ребенку. Родительские и дочерние данные должны быть сохранены в базе данных. Здесь это работает, когда код сохранения данных записан внутри l oop, то есть каждой записи за раз.

IrTbNotificationHdr save = irTbNotificationHdrRepo.save(notificationHdr); // здесьHShidr является объектом.

При попытке сохранить список объектов, он выдает следующее исключение

irTbNotificationHdrRepo.save(scheduledNotifications); // здесь scheduleNotifications - список объектов

Not-null property references a transient value - transient instance must be saved before current operation : 
om.gov.moh.irs.model.entity.notification.IrTbNotificationDtl.irTbNotificationHdr -> om.gov.moh.irs.model.entity.notification.IrTbNotificationHdr; 
nested exception is java.lang.IllegalStateException

Первый объект, имеющий отношение «один ко многим» к деталям.

@Entity
@Table(name = "IR_TB_NOTIFICATION_HDR")
public class IrTbNotificationHdr implements java.io.Serializable {

    @Id
    @SequenceGenerator(name = "SEQ_MSG_ID", sequenceName = "IR_SEQ_MSG_ID", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MSG_ID")
    @Column(name = "MSG_ID", unique = true, nullable = false, precision = 22, scale = 0)
    private Long msgId;

    @Column(name = "PREF_ID", precision = 22, scale = 0)
    private Long prefId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "irTbNotificationHdr", cascade = CascadeType.ALL)
    private Set<IrTbNotificationDtl> irTbNotificationDtls = new HashSet<IrTbNotificationDtl>(0);


    }

Второй объект, имеющий отношение ManyToOne к Hdr

@Entity
@Table(name = "IR_TB_NOTIFICATION_DTL")
public class IrTbNotificationDtl implements java.io.Serializable {

    @Id
    @SequenceGenerator(name = "SEQ_MSG_DTL_ID", sequenceName = "IR_SEQ_MSG_DTL_ID", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MSG_DTL_ID")
    @Column(name = "MSG_DTL_ID", unique = true, nullable = false, precision = 22, scale = 0)
    private Long msgDtlId;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "MSG_ID")
    private IrTbNotificationHdr irTbNotificationHdr;

    @Column(name="PERSCODE")
    private Long persCode;
    }


Service class save logic, here the individual save works fine. But when trying to save as list it fails. Commented the list saving code.

// save logi c

List<IrTbNotificationHdr> scheduledNotifications = new ArrayList<IrTbNotificationHdr>();
        IrTbNotificationHdr notificationHdr = null;
        IrTbNotificationDtl notificationDtl = null;

        List<NotificationDateDto> scheduledDates = new ArrayList<NotificationDateDto>(); 
        scheduledDates = getDates();// to get a list of date values logic

        for (NotificationDateDto val : scheduledDates) {
            notificationHdr = new IrTbNotificationHdr();
            notificationHdr.setPrefId(notificationHdrIncId.getPrefId());
            for (IrTbNotificationDtl notiDtl : notificationHdrIncId.getIrTbNotificationDtls()) {
                notificationDtl = new IrTbNotificationDtl();
                notificationDtl.setIrTbNotificationHdr(notificationHdr);
                notificationDtl.setPersCode(notiDtl.getPersCode());

            }
            notificationHdr.setIrTbNotificationDtls(notificationDtls);
            // scheduledNotifications.add(notificationHdr);
            IrTbNotificationHdr save = irTbNotificationHdrRepo.save(notificationHdr);
        }
        // List<IrTbNotificationHdr> save =
        // irTbNotificationHdrRepo.save(scheduledNotifications); // this is not working
    }
...