Java 在ModelAttribute模型中接收更新现有数据库记录,而不是创建新记录

Java 在ModelAttribute模型中接收更新现有数据库记录,而不是创建新记录,java,spring,hibernate,spring-boot,thymeleaf,Java,Spring,Hibernate,Spring Boot,Thymeleaf,为什么AgentController代码更新现有记录而不是创建新记录,但CountryController中的相同代码工作正常 国家主计长: @GetMapping("/country/add") public String addNewCountry(Model model){ model.addAttribute("country", new Country()); return "country/add"; } @PostMapping("/country") publi

为什么AgentController代码更新现有记录而不是创建新记录,但CountryController中的相同代码工作正常

国家主计长:

@GetMapping("/country/add")
public String addNewCountry(Model model){
    model.addAttribute("country", new Country());
    return "country/add";
}

@PostMapping("/country")
public String createCountry(@ModelAttribute Country country){
    countriesRepository.save(country);
    return "redirect:/countries";
}
国家/地区HTML表格:

    <form action="#" th:action="@{/country}" th:object="${country}" method="post">
        <p>Name: <input type="text" th:field="*{name}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
    <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="text" th:field="*{documents}" /></p>
        <p>People recruited: <input type="text" th:field="*{people_recruited}"></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
UPD: 如果我创建第一个代理并将其删除,则下一个创建操作将正常工作

代理类

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
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;

这段代码应该是有效的。您是否自动连接了
代理
实体的实例?在这种情况下,它将更新而不是创建新记录。@AritraPaul
@Autowired
private AgentsRepository AgentsRepository
@Autowired
私有国家储蓄国家储蓄
可能不是您问题的解决方案,但您的代理表单操作行应如下所示:
请注意,没有character@Ahmet已在
@Autowired private AgentsRepository AgentsRepository
中的某个地方修复了我的问题。此代码应该可以正常工作。您是否自动连接了
代理
实体的实例?在这种情况下,它将更新而不是创建新记录。@AritraPaul
@Autowired
private AgentsRepository AgentsRepository
@Autowired
私有国家储蓄国家储蓄
可能不是您问题的解决方案,但您的代理表单操作行应如下所示:
请注意,没有character@Ahmet已在
@Autowired private AgentsRepository AgentsRepository
@Entity
@Table(name = "agents")
public class Agent implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
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;