Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA Foreign Key in Child not persisting

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`));
like image 494
kingpia Avatar asked Mar 07 '23 01:03

kingpia


1 Answers

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 :)

like image 122
Saksham Gupta Avatar answered Mar 11 '23 17:03

Saksham Gupta