我有以下实体类,代理和客户端。一个代理可以拥有多个客户端。 (一对多)
@Entity
@Table(name="MST_AGENT")
public class AgentEntity
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String id;
@OneToMany(mappedBy="agent",fetch=FetchType.LAZY,orphanRemoval=false)
private Set<ClientEntity> clients = new HashSet<>();
@Column(name="FIRST_NAME")
private String firstName;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public Set<ClientEntity> getClients()
{
return clients;
}
public void setClients(Set<ClientEntity> clients)
{
this.clients = clients;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}//Agent Entity
ClientEntity如下
@Entity
@Table(name="MST_CLIENT")
public class ClientEntity
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String id;
@Column(name="FIRST_NAME")
private String firstName;
@ManyToOne
private AgentEntity agent;
public AgentEntity getAgent()
{
return agent;
}
public void setAgent(AgentEntity agent)
{
this.agent = agent;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}//Client Entity
我在应用程序中获得了客户端主键的引用。所以我能够直接使用实体管理器查找API找到客户端实体。
但是在获取客户端之后,我需要访问父AgentEntity。
AgentEntity parentAgent = null;
ClientEntity clientEntity = null;
clientEntity = entityManager.find(ClientEntity.class, "2001");
// Parent Agent is null as well as don't know the Agent Id by anyway
parentAgent = clientEntity.getAgent();
现在,如果我将FetchType.EAGER放入,那么我只获得对parentAgent的引用。我不想使用EAGER抓取。由于我已经创建了一种双向关系,我也没有获得Agent Id的价值,因此我可以找到使用实体管理器的查找API。在这种情况下,我必须使用Criteria Query。
如果我有对其子实体的引用,请建议如何加载Parent实体。