I have two models, one for the customer and one for the seller, in the customer table I need to have only one seller, already in the seller table, I can have several customers for a single seller.
But when I try to add a new customer, setting the salesperson's id, it just doesn't work.
Here is the code:
- Seller Model:
package com.crud.spring.jpa.postgresql.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
@Entity
@AllArgsConstructor
@Getter
@Setter
@Table(name = "sellers", uniqueConstraints = @UniqueConstraint(columnNames = "code"))
public class Seller {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "code")
private long code;
@Column(name = "name")
private String name;
public Seller(){
}
}
- Client Model:
package com.crud.spring.jpa.postgresql.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import javax.persistence.*;
@Entity
@AllArgsConstructor
@Getter
@Setter
@Table(name = "clients", uniqueConstraints = @UniqueConstraint(columnNames = "code"))
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "code")
private long code;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "seller_id", referencedColumnName = "id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Seller seller;
public Client(){
}
}
- Client Controller:
@PostMapping("/clients")
public ResponseEntity createClients(@RequestBody Client client) {
return this.service.createClients(client);
}
- Client Service:
public ResponseEntity createClients(Client client) {
try {
client = this.repository.save(client);
return new ResponseEntity(client, HttpStatus.OK);
} catch (Exception e){
return new ResponseEntity(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
- Post Seller:
- Post Client:
- Error is:
"entityName": "com.crud.spring.jpa.postgresql.model.Client", "propertyName": "seller", "message": "not-null property references a null or transient value : com.crud.spring.jpa.postgresql.model.Client.seller", "localizedMessage": "not-null property references a null or transient value : com.crud.spring.jpa.postgresql.model.Client.seller", "suppressed": [] "message": "not-null property references a null or transient value : com.crud.spring.jpa.postgresql.model.Client.seller; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.crud.spring.jpa.postgresql.model.Client.seller",
enter code here"localizedMessage": "not-null property references a null or transient value : com.crud.spring.jpa.postgresql.model.Client.seller; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.crud.spring.jpa.postgresql.model.Client.seller",

