gpt4 book ai didi

Java hibernate 。创建有错误的记录

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:09 25 4
gpt4 key购买 nike

当我尝试创建注释记录时出现此错误。

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

注释实体

public class Annotation implements Identifiable {

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

@ManyToOne
@JoinColumn(name = "id_user")
private User profile;

@Column
@Temporal(TemporalType.DATE)
private Date date;

@Column
private String name;

@Column
private String description;

// Only getters and setters

}

用户实体

@Entity
@Table
@Component
public class User implements Identifiable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column
private String email;

@Column
private String username;

@Column
private String password;

@Column(nullable = false,columnDefinition = "int default 1")
@Enumerated(EnumType.ORDINAL)
private UserRole role = UserRole.ROLE_USER;

@Column
private Boolean enabled = Boolean.TRUE;

}

我知道为了避免此错误,我必须使用 CascadeType.ALL 但之后出现另一个问题:当我尝试创建注释记录时,会自动创建不应该创建的用户记录。

如何在新的注释记录中添加id_profile?

最佳答案

我通过更改解决了这个问题

@Column(name="id_user")
private Long idUser;

实例

@ManyToOne
@JoinColumn(name = "id_user")
private User profile;

但我认为这种方式是错误的

关于Java hibernate 。创建有错误的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33505228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com