【发布时间】:2015-06-13 22:43:50
【问题描述】:
我在数据表中显示订单列表,其中一列必须显示该订单的 customer_id:customer_id 是 ManyToOne 关系中的 fk。
客户实体
@Entity
public class Customer {
///other columns
@OneToMany(mappedBy = "cliente", fetch=FetchType.EAGER)
private List<Order> orders;
订单实体
@Entity
@Table(name = "orders")
public class Order implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column (nullable = false)
private String state;
@Column (nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Calendar openingDate;
@Column (nullable = true)
@Temporal(TemporalType.TIMESTAMP)
private Calendar closingDate;
@Column (nullable = true)
@Temporal(TemporalType.TIMESTAMP)
private Calendar evadingDate;
@ManyToOne(cascade={ CascadeType.PERSIST, CascadeType.REMOVE },fetch=FetchType.LAZY)
@JoinColumn(name = "customer_id")
private Customer customer;
最后,这是我将包含所有订单的数据表放入的页面。
showOrders.xhtml
<h:dataTable id="list" value="#{orderController.orders}"
var="order">
<h:column>
<f:facet name="header">Nome</f:facet>
<h:commandLink action="#{orderController.findOrder}"
value="#{order.id}" style="color: orange">
<f:setPropertyActionListener target="#{orderController.id}"
value="#{order.id}" />
</h:commandLink>
</h:column>
<h:column>
<f:facet name="header">Stato</f:facet>
<h:outputText value="#{order.state}" />
</h:column>
<h:column>
<f:facet name="header">Data Apertura</f:facet>
<h:outputText value="#{order.openingDate.time}">
<f:convertDateTime datestyle="full" type="date" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">Data Chiusura</f:facet>
<h:outputText value="#{order.closingDate.time}">
<f:convertDateTime datestyle="full" type="date" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">Data Evasione</f:facet>
<h:outputText value="#{order.evadingDate.time}">
<f:convertDateTime datestyle="full" type="date" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">ID Cliente</f:facet>
<h:commandLink action="#{registerCustomer.detailsCustomer}"
value="#{order.customer.id}" style="color: orange">
<f:setPropertyActionListener target="#{registerCustomer.id}"
value="#{order.customer.id}" />
</h:commandLink>
</h:column>
问题是,除了 customer_id 没有显示在页面上之外,一切都很好。我做错了什么? 我已经尝试了很多东西,改变了获取类型,改变了关系......但我开始认为这是一个 java 问题,即使我在互联网上无法真正找到解决方案或一些解释。有人可以帮助我吗?我被困住了:/
【问题讨论】:
-
你在哪里显示
customer_id?是这里value="#{order.customer.id}"吗?该字段被延迟获取(FetchType.LAZY)。您可能需要预取,但对于如何获取与特定客户关联的订单列表是完全不可见的。此外,在@ManyToOne关系上设置CascadeType.REMOVE没有意义。每当删除其中一个子项时,它将删除关联的父项(Customer行)。因此,根据表中外键列上设置的级联类型,其剩余的孩子可能会成为孤儿。 -
@Tiny 没错,我想在那里显示它,但它没有。您是说我应该将获取更改为渴望吗?顺便说一句,我现在会清除“CascadeType.REMOVE”,不知道它为什么会在那里(不幸的是我正在和另一个人一起工作,所以不是整个代码都是我的)
-
不。不必要地使用
FetchType.EAGER是有史以来最糟糕的情况。根据给定问题的描述猜测确切的症状是困难的。你确定#{order.customer.id}的值吗?它的值可能已正确获取,但由于某些不清楚的原因,它没有正确设置为与<f:setPropertyActionListener>关联的托管 bean 属性,即target="#{registerCustomer.id}"? -
@Tiny 我不知道是不是这样,但无论如何
<f:setPropertyActionListener>用于传递参数并执行bean 的detailsCustomer 方法。主要问题是我什至看不到数据表上的客户 ID 值 -
@Enduavon 你有 customer_id 的 set 和 get 方法:getCustomer 和 setCustomer 吗?