I'm trying to use spring data CrudRepository to persist a parent entity that includes a few child entities, however I'm having trouble getting the foreign key constraint OID in the child table to save when I "save" the parent.
So in the example below a call to the controller with the provided JSON would persist an Ocean and the 2 child Fish. However the FK OID in the Fish table isn't being set.
Starting with the JSON request to the controller here is the code. JSON REQUEST
{
"name":"Atlantic",
"fishes": [
{
"name": "blue fin tuna"
},
{
"name": "great white shark"
}
]
}
OCEAN ENTITY
@Entity
public class Ocean {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long oid;
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy =
"ocean")
private Set<Fish> fishes; //yeah maybe a bad example
FISH ENTITY
@Entity
public class Fish {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long fid;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "oid", nullable = false, updatable = false, insertable =
true)
private Ocean ocean;
private String name;
CONTROLLER
@PostMapping("/god/ocean")
public Ocean createOcean(@RequestBody Ocean ocean) throws BadRequestException
{
LOG.info("createOcean()");
return oceanDao.save(ocean);
}
REPOSITORY
public interface IOceanDao extends CrudRepository<Ocean, Long> {
}
MySql TABLES
CREATE TABLE IF NOT EXISTS `mydb`.`OCEAN` (
`OID` INT NOT NULL AUTO_INCREMENT COMMENT '',
`NAME` VARCHAR(45) NOT NULL COMMENT '',
PRIMARY KEY (`OID`) COMMENT '');
CREATE TABLE IF NOT EXISTS `mydb`.`FISH` (
`FID` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',
`OID` INT(11) NULL COMMENT '',
`NAME` VARCHAR(45) NOT NULL COMMENT '',
PRIMARY KEY (`FID`) COMMENT '',
CONSTRAINT `FK_OID`
FOREIGN KEY (`OID`)
REFERENCES `mydb`.`OCEAN` (`OID`));
I was getting the same problem until JsonManagedReference came to my rescue.
Try changing your entities to include them like this:
In the Ocean Entity:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy =
"ocean")
@JsonManagedReference
private Set<Fish> fishes;
In the Fish Entity:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "oid", nullable = false, updatable = false, insertable =
true)
@JsonBackReference
private Ocean ocean;
I was not able to find why it works this way, so please let me know if you come to know :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With