-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Soooo many "one to many" relations
PostPosted: Mon May 24, 2010 9:39 am 
Newbie

Joined: Mon May 24, 2010 9:12 am
Posts: 3
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.