gpt4 book ai didi

java - JPA EntityGraph 不工作;始终加载相关集合

转载 作者:行者123 更新时间:2023-11-29 08:45:34 26 4
gpt4 key购买 nike

我正在尝试让命名实体图正常工作。基本上,我有一个可以有零个或多个地址的客户。当我查询以获取客户列表时,我需要客户的字段,而不是关联的地址。当我查询特定客户时,我需要它的字段和所有关联的地址。所以,我一直在尝试使用命名实体图,因为它们似乎可以解决这种情况。我创建了一个来获取基本信息,另一个获取了所有信息。不幸的是,当我使用任何一个图表时,我仍然总是得到所有的东西。我确信我犯了一个简单的错误,但我似乎找不到它。非常感谢您帮助我找出错误。相关代码如下……

感谢您的宝贵时间和建议,迈克

/////////////////////////////////////////////////////////
// ENTITIES
/////////////////////////////////////////////////////////

@Entity
@Table(name = "customers")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")})

@NamedEntityGraphs({
@NamedEntityGraph(
name="previewCustomerEntityGraph",
attributeNodes={
@NamedAttributeNode("id"),
@NamedAttributeNode("displayAs"),
@NamedAttributeNode("rowVersion")
}
),
@NamedEntityGraph(
name="fullCustomerEntityGraph",
attributeNodes={
@NamedAttributeNode("id"),
@NamedAttributeNode("displayAs"),
@NamedAttributeNode("rowVersion"),
@NamedAttributeNode(value = "addressCollection", subgraph = "addressCollection")
},
subgraphs = @NamedSubgraph(
name = "addressCollection",
attributeNodes = {
@NamedAttributeNode("id"),
@NamedAttributeNode("displayAs"),
@NamedAttributeNode("rowVersion")
}
)
)
})
public class Customer implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Id")
@XmlAttribute(required=true)
private Long id;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 256)
@Column(name = "DisplayAs")
@XmlElement(required=true, nillable=false)
private String displayAs;

@Basic(optional = true)
@Size(max = 256)
@Column(name = "Name")
@XmlElement(required=true, nillable=true)
private String name;

@Basic(optional = false)
@NotNull
@XmlElement(required=true, nillable=false)
@Column(name = "RowVersion")
@Version
private Timestamp rowVersion;


@OneToMany(mappedBy = "customerId",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Collection<Address> addressCollection;

//@XmlTransient
public Collection<Address> getAddressCollection() {
return addressCollection;
}

public void setAddressCollection(Collection<Address> addressCollection) {
this.addressCollection = addressCollection;
}

// Object methods have been removed to save space
}


@Entity
@Table(name = "addresses")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a")})
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@XmlAttribute(required=true)
@Column(name = "Id")
private Long id;

@Basic(optional = false)
@NotNull
@Column(name = "RowVersion")
@Temporal(TemporalType.TIMESTAMP)
@XmlElement(required=true, nillable=true)
private Date rowVersion;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 256)
@Column(name = "DisplayAs")
@XmlElement(required=true, nillable=true)
private String displayAs;

@JoinColumn(name = "CustomerId", referencedColumnName = "Id")
@ManyToOne
private Customer customerId;

// Object methods have been removed to save space
}





/////////////////////////////////////////////////////////
// QUERIES
/////////////////////////////////////////////////////////

protected T find(Object id) {
// Filter to only return entities belonging to the tenant.

EntityGraph eg = getEntityManager().getEntityGraph("fullCustomerEntityGraph");

javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery<T> cq = cb.createQuery(entityClass);
javax.persistence.criteria.Root<T> from = cq.from(entityClass);
javax.persistence.criteria.CriteriaQuery<T> select = cq.select(from);
cq.where(cb.and(
cb.equal(from.<T>get("id"), id)
));

javax.persistence.TypedQuery<T> tq = getEntityManager().createQuery(select);

tq.setHint("javax.persistence.fetchgraph", eg);

return getSingleResultOrNull(tq);
}

protected List<T> findAll() {
// Filter to only return entities belonging to the tenant.

EntityGraph eg = getEntityManager().getEntityGraph("previewCustomerEntityGraph");

javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery<T> cq = cb.createQuery(entityClass);
javax.persistence.criteria.Root<T> from = cq.from(entityClass);
javax.persistence.criteria.CriteriaQuery<T> select = cq.select(from);
javax.persistence.TypedQuery<T> tq = getEntityManager().createQuery(select);

tq.setHint("javax.persistence.fetchgraph", eg);

return tq.getResultList();
}

protected T getSingleResultOrNull(javax.persistence.TypedQuery<T> query) {
query.setMaxResults(1);
List<T> list = query.getResultList();
if (list.isEmpty()) {
return null;
}

return list.get(0);
}

最佳答案

序列化程序正在调用 getAddressCollection

请注意,@XmlTransient 在地址列表中有注释。将此注释放回原位并重试。

在这种情况下,自动序列化总是会避开列表。如果您需要在其他地方返回完整的图表,我建议您编写自己的序列化逻辑或创建一个 Value Object 来准确保存您需要的内容,并让序列化程序完成这项工作。

关于java - JPA EntityGraph 不工作;始终加载相关集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25632292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com