0

How can I transform that command line to PostMapping method in Spring? Considering 2 Entitys, Entity Equipment(UUID id, UUID equipment_model_id, String name)and another Entity Equipment_model(UUID id,String name).

Equipment_model have an Primary key.

Equipment have a foreign key relational to primary key Equipment_model

Consider UUID from Equipment_model = a3540227-2f0e-4362-9517-92f41dabbfdf

If i write at command line:

INSERT INTO operation.equipment(id, equipment_model_id,name) VALUES(gen_random_uuid(), 'a3540227-2f0e-4362-9517-92f41dabbfdf','test')

RESULT: Query returned successfully

Howhever i'm trying some days in POST method and having same error: "message": "could not execute statement; SQL [n/a]; constraint [equipment_model_id" of relation "equipment]",

public class Equipment implements Serializable { private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID equipment_model_id;
    private String name;

//Getters and Setters 

public interface EquipmentRepository extends JpaRepository<Equipment,UUID>{

}

public class EquipmentResource {

@Autowired
EquipmentRepository equipmentRepository;

@GetMapping("/equipments")
public List<Equipment> listEquipments(){
    return equipmentRepository.findAll();
}

@PostMapping("/equipmentp")
public Equipment salvaProduto(@RequestBody  Equipment equipment) {
    return equipmentRepository.save(equipment);
}       

}

EQUIPMENT_MODEL:

public class Equipment_model implements Serializable{

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

private UUID id;
private String name;
//GETTErs Setters }

public interface Equipment_modelRepository extends JpaRepository<Equipment_model,UUID>{

}

public class Equipment_modelResource {

@Autowired
private Equipment_modelRepository equipment_modelRepository;

@GetMapping("/equipment_model")
@Operation(summary = "Create a clone", description = "Create a clone with the information of the body.")    
public List<Equipment_model> listModel(){
    return equipment_modelRepository.findAll();
}

}

0

1 Answer 1

0

This looks like you are trying to generate the equipment model in your equipment instead of relating one thing to another. Instead of defining:

    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID equipment_model_id;

you should try to reference it to:

    @ManyToOne
    @JoinColumn(name="equipment_model_id")
    private Equipment_model equipment_model;

This way, it will not try to generate a new ID that does not have a corresponding value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.