简体   繁体   English

对象引用了未保存的瞬态实例-在刷新休眠JPA之前保存瞬态实例

[英]Object references an unsaved transient instance - save the transient instance before flushing hibernate JPA

When I try to save the data this error is shown: 当我尝试保存数据时,显示此错误:

com.LTR.entity.Silicon.platform -> com.LTR.entity.Platform, com.LTR.entity.Silicon.platform-> com.LTR.entity.Platform,

It says that I need to save the transient. 它说我需要保存瞬变。

I have been trying to put cascadeType.ALL but this does not work. 我一直在尝试放置cascadeType.ALL,但这不起作用。

@Entity
@Table(name="silicon",uniqueConstraints={@UniqueConstraint(columnNames = {"silicon_visual_id"})})
@EnableTransactionManagement
@DynamicUpdate
public class Silicon implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id", unique = true, nullable = false)
    private Long id;

    @Column(name="mir", unique = false, nullable = true)
    private Long mir;

    @Column(name = "silicon_name", nullable = false, length= 45)
    private String siliconName;

    @Column(name = "type_silicon", nullable = false, length= 45)
    private String typeSilicon;

    @Column(name = "qdf", nullable = true, length= 45)
    private String qdf;

    @Column(name = "silicon_visual_id", nullable = false, length= 45)
    private String siliconVisualId;

    @Column(name = "cpu_id", nullable = true, length= 45)
    private String cpuId;

    @Column(name = "stepping", nullable = true, length= 45)
    private String stepping;

    @Column(name = "socket", nullable = true, length= 45)
    private String socket;

    @Column(name = "status_silicon", nullable = false,length= 45)
    private String statusSilicon;

    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="user_owner", nullable = false)
    private User userOwner;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = "date_admission", nullable = false)
    private Date dateAdmission;

    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="user_request", nullable = true)
    private User userRequest;

    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="user_last_returned", nullable = true)
    private User userLastReturned;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = "date_delivered", nullable = true)
    private Date dateDelivered;

    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="platform_current", nullable = true)
    private Platform platform;

    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="platform_own", nullable = true,updatable= false)
    private Platform platformOwn;

    /*GETTERS AND SETTERS*/

@Entity
@Table(name="platform")
public class Platform implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="platform_id", unique = true, nullable = false)
    private Long platformId;

    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="locationId", nullable = false)
    private Location location;

    @Enumerated(EnumType.STRING)
    @JoinColumn(name="businessUnit", nullable = true)
    private BusinessUnit businessUnit;

    @Column(name = "name", nullable = false, length= 45,unique = true)
    private String name;

    @Column(name = "project", nullable = false, length= 45)
    private String project;

    @Column(name = "serialPlatform", nullable = false, length= 45, unique 
        = true)
    private String serialPlatform;

    @Column(name = "model", nullable = false, length= 45)
    private String model;

    @Column(name = "chasisSerial", nullable = false, length= 45)
    private String chasisSerial;

    @Column(name = "chasisModel", nullable = false, length= 45)
    private String chasisModel;

    @Column(name = "ismpKitName", nullable = false, length= 45)
    private String ismpKitName;

    @Column(name = "ismpSerialNumber", nullable = false, length= 45)
    private String ismpSerialNumber;

    @Column(name = "assignedTo", nullable = false, length= 45)
    private String assignedTo;

    @Column(name = "OwnedBy", nullable = false, length= 45)
    private String OwnedBy;

    @Column(name = "ismNumber", nullable = false, length= 45)
    private int ismNumber;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = "receivedDate", nullable = false)
    private Date receivedDate;

    @Column(name = "asset", nullable = false, length= 45)
    private int asset;

    @Column(name = "startStatus", nullable = false, length= 45)
    private boolean startStatus;

    @Column(name = "finalStatus", nullable = false, length= 45)
    private boolean finalStatus;

    @Column(name = "cloudReady", nullable = false, length= 45)
    private boolean cloudReady;

    @OneToMany(mappedBy="platform")
    private List<Annotation> annotations;

    @OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL, mappedBy 
        = "platform")
    private Set<Silicon> userRole = new HashSet<Silicon>();

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "platform")
    private Host Host;

    /*GETTERS AND SETTERS*/

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.LTR.entity.Silicon.platform -> com.LTR.entity.Platform; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.LTR.entity.Silicon.platform -> com.LTR.entity.Platform
2019-09-09 08:32:43.161 ERROR 21968 --- [nio-8081-exec-9] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.LTR.entity.Silicon.platform -> com.LTR.entity.Platform]
2019-09-09 08:32:43.172  INFO 21968 --- [nio-8081-exec-9] com.LTR.controller.SiliconController     : org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.LTR.entity.Silicon.platform -> com.LTR.entity.Platform; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.LTR.entity.Silicon.platform -> com.LTR.entity.Platform
2019-09-09 09:25:45.500  WARN 8000 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
2019-09-09 09:25:45.500 ERROR 8000 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'owned_by' cannot be null
2019-09-09 09:25:45.506 ERROR 8000 --- [nio-8081-exec-1] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2019-09-09 09:25:45.517  INFO 8000 --- [nio-8081-exec-1] com.LTR.controller.SiliconController     : org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

I dont know why when i tried to update or insert this crashed with this error , and also this only happens when the platform object is null when i try to update 我不知道为什么当我尝试更新或插入时因此错误而崩溃,并且这仅在当我尝试更新时平台对象为null时发生

when i set cascadeType.ALL it sasys that the ownedBy from the platform entity is empty but in the database is no empty 当我设置cascadeType.ALL时,它使来自平台实体的ownedBy为空,但在数据库中不为空

Add cascade="all" attribute to all attributes. 将Cascade =“ all”属性添加到所有属性。

Like below 像下面

@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER, optional=true, cascade=CascadeType.ALL)
@JoinColumn(name="platform_current", nullable = true)
private Platform platform;

You need to set optional=true . 您需要设置optional=true

The @Column(nullable=true) is to instruct the DDL generation tool to include a NULL SQL column type constraint. @Column(nullable = true)指示DDL生成工具包括NULL SQL列类型约束。

For more on optional vs nullable, check out this StackOverflow answer. 有关可选与可为空的更多信息,请查看此StackOverflow答案。

You need to save Platform before saving Silicon so Persist is the CascadeType you would need here. 您需要先保存平台,然后再保存芯片,因此Persist是您在此处需要的CascadeType。

@JsonIgnore
@ManyToOne(fetch=FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(name="platform_current", nullable = true)
private Platform platform;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA JPA对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例 - JPA object references an unsaved transient instance - save the transient instance before flushing Spring Data/JPA 和 errorType :对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - Spring Data/JPA and errorType : object references an unsaved transient instance - save the transient instance before flushing 保存实体时JPA错误,对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例 - JPA error while saving entity, object references an unsaved transient instance - save the transient instance before flushing 对象引用了一个未保存的临时实例-使用休眠空间在刷新之前保存该临时实例 - object references an unsaved transient instance - save the transient instance before flushing using hibernate spatial Hibernate级联:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例 - Hibernate cascades : Object references an unsaved transient instance - save the transient instance before flushing 休眠:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例 - Hibernate : object references an unsaved transient instance - save the transient instance before flushing HIbernate - 对象引用一个未保存的瞬态实例 - 在刷新之前保存瞬态实例 - HIbernate - object references an unsaved transient instance - save the transient instance before flushing org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM