I have been trying to fetch data from 2 different tables to a single entity in JPA but with no result.
The entity that keeps data from two different tables is as below :
@Data @Entity @JsonSnakeCase
public class WareHouse {
@Id
@GeneratedValue
private long id;
@Column(unique = true)
private String fcId;
@Enumerated(EnumType.STRING)
private FCStatus status;
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy = "fcId")
private List<WorkingHours> workingHours;
@Enumerated(EnumType.STRING)
private IntegrationType integrationType;
}
The other entity WorkingHours
is :
@Data
@Entity
@JsonSnakeCase
public class WorkingHours {
@Id
@GeneratedValue
private long id;
private String fcId;
private LocalDate date;
private DateTime start;
private DateTime end;
}
The tables WareHouse
and WorkingHours
have one-to-many relationship and fcId
is the column that joins them.
In my use case, I have to fetch WareHouse
details and its WorkingHours
in a single entity WareHouse
as defined above. How do I achieve this ?
The named query (below) only fetches the WareHouse
data and WorkingHours
is coming empty. Is the data model wrong ? Or is the query wrong ? (I thought JPA would take care of automatically fetching from the related table when given the annotations OneToMany
and FetchType
etc.)
<named-query name="searchByFcId">
<query>
<![CDATA[
select f from WareHouse f where f.fcId = :fc_id
]]>
</query>
</named-query>