How to persist and query embeddable entities that have no bidirectional relationship using Jparepository

Asked

Viewed 38 times

1

Using as an example a hotel booking app, I have something like

public class Reserva{ 
    ...
  @CollectionTable(name = "custo_extras", joinColumns = @JoinColumn(name = "reserva_id"))
private List<CustoExtra> custosExtras;
   ...
}


@Embeddable
public class CustoExtra{

  private BigDecimal valor;
  private String descricao;

  ...
}

and an extra cost Repository

public interface ICustoExtraRepository extends JpaRepository<CustoExtra, Integer>

my point is, how I can query all the extra costs of a given reservation, or persist an extra cost of a reservation, since I don’t have a booking attribute in my extra cost class, which makes it impossible for me to make a method in Pository like

List<CustoExtra> findAllByReserva(Reserva reserva);
  • 1

    You have to always make the query in the entity ( in case a Reserva ), not in the CustoExtra. When using @Embeddable you are saying that your entity has that structure. If you want to consult an entity called CustoExtra, the association must be made by means of foreign key

  • 1

    I answered a question a while ago explaining these concepts of @Embeddable and @Embedded, check here

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.