Reputation: 39
I'm showing a list of Orders in a datatable , and one of its columns must show the customer_id of that order: customer_id is a fk in ManyToOne relationship.
Customer entity
@Entity
public class Customer {
///other columns
@OneToMany(mappedBy = "cliente", fetch=FetchType.EAGER)
private List<Order> orders;
Order entity
@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;
and finally, this is the page where i'm putting the datatable with all the orders.
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>
Problem is, everything is fine except the customer_id, which doesn't show on the page. What am I do wrong? I've tried looot of things, changed type of fetching, changed the relationships...but i'm start to thinking it's a java problem , even if i can't really find the solution or some explanation about it on the internet. Could someone help me? I'm stuck :/
Upvotes: 1
Views: 2248
Reputation: 39
I'm happy to say I found the solution! (kudos for @Mauricio Gracia to enlight my finding-a-solution path)
First, I removed the "fetch = FetchType.LAZY" from the customer relationship in Order class
@ManyToOne(cascade={CascadeType.PERSIST})
@JoinColumn(name = "customer_id")
private Customer customer;
Then, I serialized the Customer class
@Entity
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
And everything works fine, I can see the customer id in my .xhtml page :)) However, I don't know if this is a good practice but I think I can take the risk in a small university project like this! Thanks to everyone
Upvotes: 1
Reputation: 10862
Since your Order
is related to a Customer
here is what you need to do to still use LAZY loading but be able to initilize the customer of a given order
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();
Also check that you have the set and get related to the customer_id JOIN
Customer getCustomer() { }
setCustomer(Customer cust) { }
Upvotes: 1