Java Spring hibernate域保存不使用postgreSQL
我正在保存与Java Spring hibernate域保存不使用postgreSQL,java,spring,hibernate,Java,Spring,Hibernate,我正在保存与UserAccount有关系的Executive域对象,但在保存对象时出现一些错误: 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 unsave
UserAccount
有关系的Executive
域对象,但在保存对象时出现一些错误:
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
高管
@Column(name = "name", nullable = false)
@ManyToOne
@JoinColumn(name = "user_account_id", nullable = false)
private UserAccount userAccount;
@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;
用户帐户
@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;
我在使用执行域时使用了:
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);
UserAccount=newuseraccount();
account.setUserName(用户名);
帐户设置密码(password);
Role-Role=roleService.findRoleByRoleName(“Role_-EXECUTIVE”);
Listroles=新的ArrayList();
角色。添加(角色);
account.setRoles(角色);
MarketingExecutive executive=新的MarketingExecutive();
行政人员姓名(姓名);
执行。setUserAccount(account);
MarketingExecutive服务。保存MarketingExecutive(执行);
在当前操作之前必须保存临时实例
这意味着您正在持久化Executive
的一个实例,该实例与transient
UserAccount
实例有关系,而UserAccount
的一个实例尚未持久化
在持久化Executive
之前,您应该持久化新实例化的(transient
)UserAccount
:
UserAccount account = new UserAccount();
...
accountRepository.save(account);
...
executive.setUserAccount(account);
marketingExecutiveService.saveMarketingExecutive(executive);
或者向您的主管
实体添加合适的级联
选项:
// other annotations
@ManyToOne(cascade = CascadeType.PERSIST)
private UserAccount userAccount;
在当前操作之前必须保存临时实例
这意味着您正在持久化Executive
的一个实例,该实例与transient
UserAccount
实例有关系,而UserAccount
的一个实例尚未持久化
在持久化Executive
之前,您应该持久化新实例化的(transient
)UserAccount
:
UserAccount account = new UserAccount();
...
accountRepository.save(account);
...
executive.setUserAccount(account);
marketingExecutiveService.saveMarketingExecutive(executive);
或者向您的主管
实体添加合适的级联
选项:
// other annotations
@ManyToOne(cascade = CascadeType.PERSIST)
private UserAccount userAccount;