我在数据表中显示订单列表,其中一列必须显示该订单的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问题,即使我无法真正找到解决方案或者在互联网上对它进行一些解释。有人能帮助我吗?我被困了:/
答案 0 :(得分:0)
由于您的Order
与Customer
相关,因此您仍然需要执行LAZY加载,但能够启动给定订单的客户
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
ord = (Order) session.get(Order.class, id_order);
Hibernate.initialize(ord.getCustomer());
session.getTransaction().commit();
Customer cust = ord.getCustomer();
同时检查您是否拥有该设置并与customer_id JOIN相关
Customer getCustomer() { }
setCustomer(Customer cust) { }
答案 1 :(得分:0)
我很高兴地说我找到了解决方案! (感谢@Mauricio Gracia为我的发现提供了解决方案)
首先,我从Order类中的客户关系中删除了“fetch = FetchType.LAZY”
@ManyToOne(cascade={CascadeType.PERSIST})
@JoinColumn(name = "customer_id")
private Customer customer;
然后,我序列化了Customer类
@Entity
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
一切正常,我可以在我的.xhtml页面看到客户ID :)) 但是,我不知道这是不是一个好习惯,但我想我可以冒这样的小型大学项目的风险!谢谢大家