Hello, i'm new to both Hibernate and this forum.
I have a database schema with a lot of foreign key constrains; when i run my reveng hibernate tool task, it create a one-to-many relation for every single foreign key declared in the database schema.
Is it advisable to mantain all of these collections/sets or it would be better to remove the ones I suppose that will be not useful?
For example, I have to manage a table ShippingOrders and another one OrderState.
ShippingOrder has a foreign key column on table OrderState (to specify if the order is released or is waiting.. etc..)
In entity OrderState reveng tool task creates this onetomany set (eventually easily replaceable with a collection):
Code:
public class OrderState {
...
private Set<ShippingOrder> shippingOrders = new HashSet<ShippingOrder>(0);
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "orderState")
public Set<ShippingOrder> getShippingOrders() {
return this.shippingOrders;
}
public void setShippingOrders(Set<ShippingOrder> shippingOrders) {
this.shippingOrders = shippingOrders;
}
...
}
My doubt is that if I define all of these one-to-many collections/sets I have to manage both ends of the associations every time I change the foreign key side of the relation, e.g. if I want to switch from orderState "waiting" to "execution" i have to do something like this:
Code:
ShippingOrder o = ...;
OrderState newState = ...; // get from database or load a proxy for the state "execution"
o.setOrderState(newState);
My o.setOrderState() would be:
Code:
public class ShippingOrder {
...
private OrderState orderState;
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "orderState", nullable = false)
public OrderState getOrderState() {
return orderState;
}
public void setOrderState(OrderState state) {
/* !!!!!!!!!!!!! */
if (getOrderState() != null) {
getOrderState().getShippingOrders().remove(this);
}
this.orderState = state;
if (state != null) {
state.getShippingOrders().add(this);
}
/* !!!!!!!!!!!! */
}
...
}
I understand why I have to do these operations but I'm wondering if that code will decrease performances too much. In my code I have to hit the ShippingOrders database table to get the ones with an order state equals mine, only to remove the current order from the collection getOrderState().getShippingOrders()!
Please explain me if I'm wrong..
Thank you in advance
Luca