У меня четыре класса: Alien
, AlienAppartment
, AlienBuilding
и AlienBedroom
. Все они имеют отношение ManyToOne
:
Alien -> has many AlienBuilding -> has many AlienAppartment -> has many AlienBedroom
Я пытаюсь вставить JSON с объектом AlienBuilding
через этот контроллер:
AlienController. java:
@CrossOrigin
@RestController
public class AliensController {
@Autowired
private AlienBuildingRepository alienBuildingRepository;
@Autowired
private AlienRepository alienRepository;
@Autowired
private AlienBedroomRepository alienBedroomRepository;
@Autowired
private AlienAppartmentRepository alienAppartmentRepository;
...
@GetMapping("/api/aliens/buildings/insert")
@Transactional
public String insertBuilding(
@RequestBody AlienBuilding alienBuilding) {
alienBuildingRepository.save(alienBuilding);
return "OK";
}
}
JSON:
{"id":null,"alien":{"id":null,"name":"some alien"},"appartments":[{"id":null,"alienBedrooms":[{"id":null,"description":"cool"}]}]}
Однако все, что я получаю, это
Hibernate: select nextval ('hibernate_sequence')
вывод в консоли . Однако в отладчике я вижу, что JSON был сопоставлен с объектом AlienBuilding
.
Я могу попытаться заставить его вставить некоторые вещи через
@GetMapping("/api/aliens/buildings/insert")
@Transactional
public String insertBuilding(
@RequestBody AlienBuilding alienBuilding) {
//alienBuildingRepository.save(alienBuilding);
alienRepository.save(alienBuilding.getAlien());
for (AlienAppartment appartment: alienBuilding.getAppartments()) {
for (AlienBedroom bedroom: appartment.getAlienBedrooms()) {
alienBedroomRepository.save(bedroom);
alienBedroomRepository.flush();
}
alienAppartmentRepository.save(appartment);
alienAppartmentRepository.flush();
}
alienBuildingRepository.save(alienBuilding);
alienBuildingRepository.flush();
return "OK";
}
, и я бы получите этот вывод:
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into aliens (name, id) values (?, ?)
Hibernate: insert into alien_bedrooms (appartment_id, description, id) values (?, ?, ?)
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into alien_appartments (building_id, id) values (?, ?)
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into alien_buildings (alien_id, id) values (?, ?)
Но building_id
столбец в alien_appartments
таблице будет пуст, как и столбец appartment_id
в alien_bedrooms
таблице
Это мои объекты:
Чужой. java:
@Entity
@Table(name = "aliens")
public class Alien {
@Id @GeneratedValue
private int id;
public Alien() {}
@Column
private String name;
@OneToMany(mappedBy = "alien", fetch=FetchType.EAGER)
@JsonIgnore
private Set<AlienBuilding> buildings;
/**
* Getters,setters
*/
Чужой. java:
@Entity
@Table(name = "alien_appartments")
public class AlienAppartment {
@Id
@GeneratedValue(strategy= GenerationType.AUTO )
public Long id;
public AlienAppartment() {
}
@OneToMany(mappedBy = "appartment", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<AlienBedroom> alienBedrooms;
@ManyToOne
@JoinColumn(name = "building_id")
@JsonIgnore
private AlienBuilding building;
/**
* Getters,setters
*/
AlienBedroom. java:
@Entity
@Table(name = "alien_bedrooms")
public class AlienBedroom {
@Id
@GeneratedValue(strategy= GenerationType.AUTO )
private Long id;
public AlienBedroom() {}
@Column
private String description;
@ManyToOne
@JoinColumn(name = "appartment_id")
@JsonIgnore
private AlienAppartment appartment;
/**
* Getters,setters
*/
AlienBuilding. java:
@Entity
@Table(name = "alien_buildings")
public class AlienBuilding {
@Id
@GeneratedValue
public Long id;
public AlienBuilding() {
}
@ManyToOne
@JoinColumn(name = "alien_id")
private Alien alien;
@OneToMany(mappedBy = "building", fetch = FetchType.EAGER)
private Set<AlienAppartment> appartments;
/**
* Getters, setters
*/
Хранилища :
@Repository
public interface AlienAppartmentRepository extends
JpaRepository<AlienAppartment, Long> {
}
@Repository
public interface AlienBedroomRepository extends JpaRepository<AlienBedroom, Long> {
}
@Repository
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
public interface AlienBuildingRepository
extends JpaRepository<AlienBuilding, Long> {
}
@Repository
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
public interface AlienRepository extends JpaRepository<Alien, Long> {
}