我需要在一定条件下将2个表连接到一个对象中.我有以下内容:
@Entity
@Table(name = "polling")
public class Polling extends DomainIdObject {
@ManyToOne
@JoinColumn(name = "owner_id")
private Person owner;
@Column(name = "poll_name")
private String name;
@Column(name = "description")
private String description;
@ManyToMany(targetEntity = PollingSchedule.class, mappedBy = "polling", fetch = FetchType.EAGER)
private List variants;
@Column(name = "start_time")
private LocalDateTime startTime;
@Column(name = "end_time")
private LocalDateTime endTime;
//getters and setters
@Entity
@Table(name = "polling_schedule")
public class PollingSchedule extends DomainIdObject {
@JoinColumn(name = "polling_id")
private Polling polling;
@Column(name = "poll_var")
private String pollingVariant;
//gettters and setters
但是当我执行以下代码时:
Query query = getEntityManager().createNativeQuery("SELECT * FROM polling p WHERE p.id=1", Polling.class);
List list = query.getResultList();
List variants = ((Polling) list.get(0)).getVariants();
变体列表为空. DB中的表如下所示:
polling
|id|owner_id|poll_name|description|start_time|end_time|
polling_schedule
|id|polling_id|poll_var|
因此,结果是我希望轮询对象仅包含那些在polling_schedule表中具有相应的polling_id的PollingVariants.
我尝试使用Filter,SecondaryTable批注,但是它对我不起作用(或者我不正确地使用它).
我使用hibernate4和spring boot 1.5.1
有人可以帮我吗?