Gustavo Gustavo - 3 months ago 23
reST (reStructuredText) Question

Why the same id is incremented when I save an object in the repository?

Whenever I call save() method the same ID is shared between three different entities and I don't know why ?

@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;
}
}


And this is my Controller:

@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);
}
}


This should happen only if the id would be static, but It's not. If I create two new Location() objects, when I will create a new Department() the Id of the department will be 3. Why ?

Answer

Since you didn't specify the strategy for @GeneratedValue, I guess that Hibernate uses the same sequence for all your entities.

You can set something like

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="department_seq_gen")
@SequenceGenerator(name="department_seq_gen", sequenceName="DEPARTMENT_SEQ")

on Department entity, and something similar on Location entity (just use location_seq_gen and LOCATION_SEQ).