I am creating a quarkus application where a certain PriceRequest
entity should be stored in the DB. This is a short version of the entity
@Entity
@Table(name = "t_price_request")
public class PriceRequest extends PanacheEntity {
@Column
public String buildingType;
@Column
public int squareMeters;
}
Now, I would like to test a post call which tries to save a PriceRequest into the DB. It calls the addPriceRequest
function from the repository which you can find below.
@ApplicationScoped
public class PriceRequestRepository implements PanacheRepository<PriceRequest> {
@Transactional
public void addPriceRequest(PriceRequest priceRequest) {
priceRequest.setCreationDate(LocalDateTime.now());
priceRequest.persistAndFlush();
}
@Transactional
public PriceRequest findPriceRequestById(Long id) {
return findById(id);
}
@Transactional
public List<PriceRequest> findAllPriceRequests() {
return findAll().stream().toList();
}
}
When testing this via a Postman call, everything works. When testing this with the OpenAPI UI, the following error pops up:
HTTP Request to /price-request failed, error id: a5caae7f-5daa-49ef-8358-0e57b9895a1a-6: org.hibernate.PersistentObjectException: detached entity passed to persist: be.backeit.domain.PriceRequest
at org.hibernate.event.internal.DefaultPersistEventListener.persist(DefaultPersistEventListener.java:88)
Whats wrong in my code? Do I need to add some extra annotations?
There are two different approaches using of Panache within the Quarkus application:
If you use PanacheEntity you shouldn't use PanacheRepository. And vice versa don't extend PanacheEntity if you work with PanacheRepository
So I think the problem is you mixed both approaches in your addPriceRequest() method.