0
save()メソッドを呼び出すと、同じIDが3つの異なるエンティティ間で共有され、その理由がわかりません。オブジェクトをリポジトリに保存すると、同じIDが増分されるのはなぜですか?
@Entity
public class Department {
@Id
@GeneratedValue
private Long departmentId;
private String name;
public Department(Long departmentId) {
this.departmentId = departmentId;
}
public Department() {
}
public Department(String name) {
this.name = name;
}
public Long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
public class Location {
@Id
@GeneratedValue
private Long locationId;
private String name;
public Location(Long locationId) {
this.locationId = locationId;
}
public Location() {
}
public Location(String name) {
this.name = name;
}
public Long getLocationId() {
return locationId;
}
public void setLocationId(Long locationId) {
this.locationId = locationId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
そして、これは私のコントローラである:
@RestController
public class SettingsController {
@Autowired
private LocationRepository locationRepository;
@Autowired
private DepartmentRepository departmentRepository;
@Autowired
private RoleRepository roleRepository;
@RequestMapping(value = "/api/locations", method = RequestMethod.POST)
public ResponseEntity addLocation(@RequestBody DataForm dataForm) {
if (dataForm == null) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
locationRepository.save(new Location(dataForm.getName()));
return new ResponseEntity(HttpStatus.CREATED);
}
@RequestMapping(value = "/api/roles", method = RequestMethod.POST)
public ResponseEntity addRole(@RequestBody DataForm dataForm) {
if (dataForm == null) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
roleRepository.save(new Role(dataForm.getName()));
return new ResponseEntity(HttpStatus.CREATED);
}
@RequestMapping(value = "/api/departments", method = RequestMethod.POST)
public ResponseEntity addDepartment(@RequestBody DataForm dataForm) {
if (dataForm == null) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
departmentRepository.save(new Department(dataForm.getName()));
return new ResponseEntity(HttpStatus.CREATED);
}
}
これは、IDは静的になる場合にのみ発生するはずですが、それはありません。 2つの新しいLocation()オブジェクトを作成すると、新しいDepartment()を作成するときに、その部門のIDは3になります。なぜですか?