I am new to Hibernate and was hoping I can gather some feedback on using a lookup table. Reading posts in the forum, it seems like a simple ManyToOne mapping does the trick, however, I am not sure if my code (see below) can be improved upon. My main concern is that I'm not sure if an enumeration would be better; also, I hate hard-coding those ids but I can't think of any other way to do this.
Any feedback / suggestions would be appreciated.
Sample client code:
Code:
OrderStatus os = OrderStatus.INIT_STATUS;
Order order = new Order();
order.setOrderStatus(os);
manager.persist(order);
The "lookup" or "reference" data entity:Code:
@Entity
public class OrderStatus implements Serializable {
@Id
private Long id;
private String status;
public final static OrderStatus INIT_STATUS = new OrderStatus(new Long(1), "Initialized");
public final static OrderStatus PENDING_STATUS = new OrderStatus(new Long(2), "Pending");
public final static OrderStatus CANCELLED_STATUS = new OrderStatus(new Long(3), "Cancelled");
public OrderStatus(Long id, String status){
this.id = id;
this.status = status;
}
... // other getters/setters not shown
}
The parent entity class, with a reference to one orderStatus entity:Code:
@Entity
public class Order implements java.io.Serializable{
private int orderId;
private OrderStatus orderStatus;
@ManyToOne
public OrderStatus getOrderStatus() {
return orderStatus;
}
... // other getters/setters not shown
}