0

I have two tables :

societe (
   id SERIAL PRIMARY KEY,
   ...,
   fk_adresse int NOT NULL
);

(fk_adresse is a foreign key)

The other table is

bureau (
    fk_societe INT PRIMARY KEY NOT NULL,
    ...,
    fk_convention INT NOT NULL,
    ....
 );

fk_societe is both the primary key of the "bureau" table and a foreign key towards the "societe" table and fk_convention is a foreign key

My Java mapping for bureau is :

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "fk_societe", nullable = false)
private Integer fkSociete;

@OneToOne(fetch = FetchType.LAZY, optional = true, cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "fk_societe", referencedColumnName = "id") })
private Societe societe;

....

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_convention", referencedColumnName = "id")
private Convention convention;

....

My controller is

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded;application/json")
public void createBureau(Bureau bureau){
    bureauRepository.save(bureau);
}

@GetMapping("/bureaux")
public List<Bureau> getAllBureaux() {
    return bureauRepository.findAll();
}

//recherche de bureau
@GetMapping("/{id}")
public ResponseEntity<Bureau> getBureauById(@PathVariable(value = "id") Integer id)
    throws ResourceNotFoundException {
        
    Bureau bureau = bureauRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Bureau not found");
    return ResponseEntity.ok().body(bureau);                                                        
}

The search of one bureau and the list of all the bureau work fine but the creation of one bureau does not work. In my societe table I have a societe with the id 42 so I send a create (POST) request with Postman and with the following data :

fk_societe          42  
fk_devise            1
fk_langue           67
fk_convention        2
est_4_e_directive   true
est_transitoire     false

Postman says to me that there is a 500 error and in my console I have the following error

2020-10-16 11:50:07.115 ERROR 8368 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne ½ fk_societe ╗ dans la relation ½ bureau ╗
DÚtailá: La ligne en Úchec contient (null, 0, 0, null, f, f).

which is translated by

ERROR: a NULL value violates the NOT NULL constraint of the fk_societe column in the bureau relationship
Details : the failing line contains (null, 0, 0, null, f, f).

Maybe my foreign keys are not correctly mapped...

Jean
  • 1
  • 1

1 Answers1

0

Try to correct your mapping for the bureau in the following way:

@Id
@Column(name = "fk_societe", nullable = false)
private Integer fkSociete;

@MapsId
@OneToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "fk_societe")
private Societe societe;

As you use the same column fk_societe as the entity id and the foreign key to the other entity, you should use the @MapsId annotation here.

SternK
  • 11,649
  • 22
  • 32
  • 46
  • Thank you for your answer. I get a new error : nested exception is org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.gpsa.refontebcf.entities.Bureau.societe]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.gpsa.refontebcf.entities.Bureau.societe]] with root cause. It looks like https://stackoverflow.com/questions/11104897/hibernate-attempted-to-assign-id-from-null-one-to-one-property-employee – Jean Oct 16 '20 at 12:40
  • I've just realized that the bureau argument is always null when I consume the REST API of bureau creation – Jean Oct 16 '20 at 14:04
  • My mapping assume that the `societe` should not be `null` and the id of the `Societe` entity is auto generated or not null. – SternK Oct 16 '20 at 14:16
  • Yes, of course. But for a reason I am missing the fk_societe and the fk_convention mapped from the JSON request message are null... – Jean Oct 16 '20 at 22:01