boy*_*od3 0 java spring hibernate
我正在保存Executive
与之关系的域对象UserAccount
,但在保存对象时会抛出一些错误:
WARN : org.hibernate.action.internal.UnresolvedEntityInsertActions - HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([com.sar.customerplus.entity.UserAccount#0])
Dependent entities: ([[com.sar.customerplus.entity.MarketingExecutive#300]])
Non-nullable association(s): ([com.sar.customerplus.entity.MarketingExecutive.userAccount])
org.springframework.dao.InvalidDataAccessApiUsageException: Not-null property references a transient value - transient instance must be saved before current operation : com.sar.customerplus.entity.MarketingExecutive.userAccount -> com.sar.customerplus.entity.UserAccount; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : com.sar.customerplus.entity.MarketingExecutive.userAccount -> com.sar.customerplus.entity.UserAccount
Run Code Online (Sandbox Code Playgroud)
行政人员
@Column(name = "name", nullable = false)
@ManyToOne
@JoinColumn(name = "user_account_id", nullable = false)
private UserAccount userAccount;
Run Code Online (Sandbox Code Playgroud)
用户帐号
@ManyToMany(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "useraccount_id") }, inverseJoinColumns = {
@JoinColumn(name = "role_id") })
private List<Role> roles;
Run Code Online (Sandbox Code Playgroud)
角色
@Id
@SequenceGenerator(name = "seq_role_gen", sequenceName = "seq_role")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_role_gen")
@Column(name = "id", nullable = false, length = 11)
private Long roleId;
@Column(name = "role_name", nullable = false, length = 20)
private String roleName;
Run Code Online (Sandbox Code Playgroud)
我正在使用执行域:
UserAccount account=new UserAccount();
account.setUserName(userName);
account.setPassword(password);
Role role=roleService.findRoleByRoleName("ROLE_EXECUTIVE");
List<Role>roles=new ArrayList<>();
roles.add(role);
account.setRoles(roles);
MarketingExecutive executive=new MarketingExecutive();
executive.setName(name);
executive.setUserAccount(account);
marketingExecutiveService.saveMarketingExecutive(executive);
Run Code Online (Sandbox Code Playgroud)
必须在当前操作之前保存瞬态实例
这意味着,你坚持的一个实例,Executive
其中有一个关系transient
UserAccount
的实例,实例的UserAccount
已没有尚未持久.
您应该在持久化之前保留新实例化的(transient
):UserAccount
Executive
UserAccount account = new UserAccount();
...
accountRepository.save(account);
...
executive.setUserAccount(account);
marketingExecutiveService.saveMarketingExecutive(executive);
Run Code Online (Sandbox Code Playgroud)
或者Cascade
为您的Executive
实体添加合适的选项:
// other annotations
@ManyToOne(cascade = CascadeType.PERSIST)
private UserAccount userAccount;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2883 次 |
最近记录: |