我在 java ee6 环境中使用 JPA2 和 hibernate 作为提供程序。
我有一个一对多的关系,当让一侧的所有行显示在 JSF 页面中时,我想显示一些多侧的属性,**但不是全部 **,甚至避免加载这些属性时填充许多侧面实体,我不知道该怎么做。
这是一个片段
我想获取表 A 的所有行并将它们显示在 jsf 表中,当显示表 A 的行时,我还想在同一个 jsf 表中显示表 B 中的一些属性,理想情况下甚至不检索/存储表中未使用的属性乙
TBL_A
________________
int ID
varchar FIRSTNAME
varchar LASTNAME
第二个是
TBL_B
___________
int ID
varchar VERSION // display on jsf page
varchar XML_DATA //large, don't want to load when getting results for A/B join
int A_ID
和我的 JPA2 关联类是这样的
@Entity
@Table(name = "TBL_A")
@NamedQueries // not sure what sql to create to accomplish this?
({ @NamedQuery(name = "A_and_B.findAll", query = "SELECT a FROM EntityA a") })
public class EntityA implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@NotNull
@Column(name = "ID", nullable = false)
private Integer id;
.
@Column(name = "FIRSTNAME")
private String firstname;
.
@Column(name = "LASTNAME")
private String lastname;
.
@OneToMany(mappedBy = "entityA", fetch = FetchType.EAGER)
private List<EntityB> entityBList;
我的 TBL_B 关联实体是
@Entity
@Table(name = "TBL_B")
public class EntityB implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@NotNull
@Column(name = "ID", nullable = false)
private Integer id;
.
@Column(name = "VERSION")
private String version;
.
@Column(name = "XML_DATA")
private String xmlData;
.
@JoinColumn(name = "A_ID", referencedColumnName = "ID")
@ManyToOne
private EntityA entityA;
I have a JSF page that displays a datatable/list of TBL_A entries and also the VERSION column from TBL_B, when getting all the rows for TBL_A.
I had to use the fetch type of EAGER to avoid hibernate lazy instatiation error to do that. hope thats the correct way to do it.
when loading all the TBL_B entities I guess I don't mind eagerly loading all TBL_B rows in entityBList, but I don't want the list of EntityB beans to have thier xmlData property loaded cause it is significantly large.
can someone explain how I can do this?
how can i make this happen with my named query
public List<EntityA> findAll_A_Entities()
{
return em.createNamedQuery("A_and_B.findAll", EntityA.class).getResultList();
}