Let’s say we have two tables, ORDERS and OFFERS
Order POJO
@Entity
@Table(name = "ORDERS")
public class Order {
@Column(columnDefinition = "serial")
private @Id Integer id;
private @NotNull @Email String ownerEmail;
private @NotNull Integer offerId;
}
Offer POJO
@Entity
@Table(name = "OFFERS")
public class Offer {
@Column(columnDefinition = "serial")
private @Id Integer id;
private @Email @NotNull String ownerId;
}
Is it possible to fetch in single query on EntityManager Order by ID so that the result will contain related Offer object?
Like creating @Transient Offer field in Order, because we don’t want to modify Offer data on persisting Order.
Or the only way is to create two queries?
Annotating
@Transienton theofferfield inOrdermeans that this field will be not mapped and persisted . Indeed , you should map this field but make it as read-only by setting its @Column.insertable() and @Column.updatable() to false.To fetch the order along with its related offer in a single query , you can use the fetch join :