gpt4 book ai didi

java - Controller 和 hibernate 集成。未保存实体

转载 作者:行者123 更新时间:2023-12-01 11:22:48 25 4
gpt4 key购买 nike

我对 Hibernate 提供的 JPA 有问题。这是我的实体:

@Entity
public class Action {
@Id
@GeneratedValue
private Integer id;

@Column
private String name;

@JoinColumn
@ManyToOne
private User user;

public Integer getId() { return this.id; }
protected void setId(Integer id) { this.id = id; }

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

public void getUser() { return this.user; }
protected void setUser(User user) { this.user = user; }
}
@Entity
public class User {
@Id
@GeneratedValue
private Integer id;

@Column
private String name;

@OneTomany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Action> actions = new ArrayList<>();

public Integer getId() { return this.id; }
protected void setId(Integer id) { this.id = id; }

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

public void addAction(Action action) {
action.setUser(this);
actions.add(action);
}
public void removeAction(Action action) {
actions.remove(action);
action.setUser(null);
}
public List<Action> getActions() { return Collections.unmodifiableList(actions); }
}

这是我的 Controller 代码:

@Controller
@RequestMapping("/user/{username}")
@SessionAttributes("user")
public class ActionController {
@Autowired
private TestService service;

@ModelAttribute("user")
public User contextRegistration(@PathVariable("username") String username) {
return this.service.getByName(username);
}

@RequestMapping("")
public String userView() {
return "user";
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String addAction(@ModelAttribute Action action) {
return "addAction";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String postAddAction(@Valid Action action, BindingResult result, User user, SessionStatus sessionStatus) {
if(result.hasErrors()) {
return "addAction";
}

user.addAction(action);
this.service.saveAction(action);
sessionStatus.setComplete();
return "redirect:/user/{username}";
}
}

当执行 postAddAction 方法时,出现以下错误:

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : pl.zaprogramowany.test.model.Action.user -> pl.zaprogramowany.test.model.User

在执行方法 saveAction 的行中。

我的代码是以此代码为蓝本的 https://github.com/spring-projects/spring-petclinic/blob/master/src/main/java/org/springframework/samples/petclinic/web/PetController.java#L79但我不知道为什么会出现此错误,因为我的代码与此非常相似。

有人可以帮忙吗?

@编辑

我正在使用 spring Jpa 存储库,服务层中的代码非常简单。

@Transactional(readyOnly = true)
public User getByName(String name) {
return this.userRepository.getByName(name);
}

@Transactional
public void saveAction(Action action) {
this.actionRepository.save(action);
}

最佳答案

当您的集合映射中未包含 cascade="all"(如果使用 xml)或 cascade=CascadeType.ALL(如果使用注释)时,就会出现此问题。

在您的情况下存在,但在用户实体而不是在操作实体

因此我们需要了解如何保存操作实体,在此基础上我们可以决定是否需要在操作实体上添加级联

关于java - Controller 和 hibernate 集成。未保存实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052831/

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