gpt4 book ai didi

java - transient 属性值异常 : object references an unsaved transient instance - save the transient instance before flushing

转载 作者:行者123 更新时间:2023-11-29 07:30:11 26 4
gpt4 key购买 nike

我正在尝试将用户详细信息存储到下表中:user、role、user_role。尝试保存详细信息时,它会引发以下错误。

Error during managed flush [org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing

这里user_role是父表,user和role表是子表。我尝试使用 cascade = CASCADE.ALL。即使它抛出了同样的错误。

用户.java

@Entity
public class User implements UserDetails, Serializable{

private static final long serialVersionUID = 902783495L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="Id", nullable=false, updatable = false)
private Long id;

private String username;
private String password;
private String email;

private boolean enabled = true;

@OneToMany(mappedBy = "user", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore
private Set<UserRole> userRoles = new HashSet<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}



public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Set<UserRole> getUserRoles() {
return userRoles;
}

public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {

Set<GrantedAuthority> authorities = new HashSet<>();
userRoles.forEach(ur -> authorities.add(new Authority(ur.getRole().getName())));

return authorities;
}

@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isEnabled() {
return enabled;
}




}

角色.java

@Entity
public class Role implements Serializable{

private static final long serialVersionUID = 890245234L;

@Id
private int roleId;

private String name;

@OneToMany(mappedBy = "role", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserRole> userRoles = new HashSet<>();

public Role(){}

public int getRoleId() {
return roleId;
}

public void setRoleId(int roleId) {
this.roleId = roleId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<UserRole> getUserRoles() {
return userRoles;
}

public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}


}

用户角色.java

@Entity
@Table(name="user_role")
public class UserRole implements Serializable {
private static final long serialVersionUID = 890345L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long userRoleId;

public UserRole () {}

public UserRole (User user, Role role) {
this.user = user;
this.role = role;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.EAGER)
private Role role;

public long getUserRoleId() {
return userRoleId;
}

public void setUserRoleId(long userRoleId) {
this.userRoleId = userRoleId;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}


}

最佳答案

这里 role_id 是主键,你每次都试图将 1 设置为 role_id,用 @ID @GeneratedValue(策略= GenerationType.AUTO) 私有(private)长序列号;

关于java - transient 属性值异常 : object references an unsaved transient instance - save the transient instance before flushing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51720120/

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