Reputation: 145
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:
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(){
}
}
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(){
}
}
@PostMapping("/clients")
public ResponseEntity createClients(@RequestBody Client client) {
return this.service.createClients(client);
}
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);
}
}
"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",
Upvotes: 1
Views: 323
Reputation: 71
So , there are few corrections : When your payload contains existing seller's id :
{
"code" : 216,
"name" : "addclientforseller5",
"seller" :
{
"id" : 5
}
}
public Client createClients(Client client) {
Long sellerId = client.getSeller().getId();
Optional<Seller> existingSeller = sellerRepository.findById(sellerId);
if (existingSeller.isPresent()) {
Seller savedSellerObject = existingSeller.get();
client.setSeller(savedSellerObject);
} else {
// throw exception
}
return clientSellerRepository.save(client); // you can return ResponseEntity also
}
When your payload contains Seller object to create new Seller do this :
{
"code" : 112,
"name" : "Pure",
"seller" :{
"code" : 1111,
"name" : "Guava"
}
}
@Autowired
ClientRepository clientRepository;
@Autowired
SellerRepository sellerRepository;
public Client createClients(Client client) {
Seller seller = new Seller();
seller.setCode(client.getSeller().getCode());
seller.setName(client.getSeller().getName());
Seller savedSeller = sellerRepository.save(seller);
client.setSeller(savedSeller);
return clientRepository.save(client);
}
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "seller_id", referencedColumnName = "id")
@OnDelete(action = OnDeleteAction.CASCADE)
//@JsonIgnore
private Seller seller;
Upvotes: 1
Reputation: 68
I've simulated your code & have the below observations to make it work.
try {
Optional<Seller> seller = sellerRepository.findById(client.getSeller().getId());
if(seller.isPresent()){
client.setSeller(seller.get());
client = this.repository.save(client);
}
return new ResponseEntity(client, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
In the above code, we use the seller id from our request client model & get the seller object from db. Then we set the reference to the client model.
{
"id": 1,
"code": 2,
"name": "Client_1",
"seller": {
"id": 1
}
}
Note: You'll have to create a valid seller first before creating the client by passing the seller id.
Upvotes: 1