自动连线存储库更新现有实体,而不是创建一个新实体

时间:2019-02-03 20:49:23

标签: java spring hibernate spring-boot

在我创建第一个座席之后在国家中,然后我无法创建新座席,它只是更新第一个座席。如果我创建第一个特工然后将其删除,则下一个特工会很好。我在休眠设置中打开了调试模式,这表明休眠确实更新了数据库中的现有记录,而不是创建新记录。

存储库

@Repository
public interface AgentsRepository extends JpaRepository<Country, Long> {
}

控制器

@Autowired
private AgentsRepository agentsRepository;

@Autowired
private CountriesRepository countriesRepository;

// Add new agent
@GetMapping("/country/{id}/agent/add")
public String addNewAgent(@PathVariable(name = "id") String countryId, Model model){
    model.addAttribute("country_id", countryId);
    model.addAttribute("agent", new Agent());
    return "agent/add";
}

@PostMapping("/country/{id}/agents/new")
public String createAgent(@PathVariable(value = "id") String countryId, @ModelAttribute Agent agent){
    return countriesRepository.findById(Long.parseLong(countryId)).map(country -> {
        agent.setCountry(country);
        agentsRepository.save(agent);
        return "redirect:/country/" + countryId + "/show/agents";
    }).orElseThrow(() -> new ResourceNotFoundException("CountryId " + countryId + " not found"));
}

国家班级

@Entity
@Table(name = "countries")
public class Country implements Serializable {

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

@NotNull
private String name;

代理类

@Entity
@Table(name = "agents")
public class Agent implements Serializable {

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

@NotNull
private String status = "classified";

@NotNull
private String first_name;

@NotNull
private String last_name;

@NotNull
@Max(value = 2147483647)
private Integer documents;

@NotNull
@Max(value = 8)
private Integer people_recruited;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "country_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Country country;

代理添加HTML

    <form action="#" th:action="@{'/country/{id}/agents/new'(id=${country_id})}" th:object="${agent}" method="post">
        <p>Name: <input type="text" th:field="*{first_name}" /></p>
        <p>Surname: <input type="text" th:field="*{last_name}"></p>
        <p>Documents: <input type="number" max="2147483647" th:field="*{documents}" /></p>
        <p>People recruited: <input  type="number" max="8" th:field="*{people_recruited}"></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>

2 个答案:

答案 0 :(得分:0)

@ModelAttribute Agent agent

您是否通过模型(UI)传递了任何agentId? 或尝试在控制器中将其设置为null。 UI很可能会为Agent发送一些虚拟ID,这对于所有Agents都是相同的,这就是JPA更新它的原因。

答案 1 :(得分:0)

尝试一下:

@PostMapping("/country/{id}/agents/new")
public String createAgent(@PathVariable(value = "id") String countryId, @ModelAttribute Agent agent){
    return countriesRepository.findById(Long.parseLong(countryId)).map(country -> {
        Agent agentToBeSaved = new Agent();
        agentToBeSaved.setCountry(country);
        agentToBeSaved.setStatus(agent.getStatus());
        agentToBeSaved.setFirstName(agent.getFirstName());
        agentToBeSaved.setDocuments(agent.getDocuments());
        agentToBeSaved.setPeopleRecruited(agent.getPeopleRecruited());
        agentsRepository.save(agentToBeSaved);
        return "redirect:/country/" + countryId + "/show/agents";
    }).orElseThrow(() -> new ResourceNotFoundException("CountryId " + countryId + " not found"));
}

如果可行,那么您正在从UI向ID对象传递一些ID。