-->
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.  [ 3 posts ] 
Author Message
 Post subject: OrderColumn index is null after updating the entity
PostPosted: Wed Nov 13, 2013 7:15 am 
Newbie

Joined: Wed Nov 13, 2013 7:03 am
Posts: 4
Hi community,

I have a problem with indexed lists. I have a bidirectional relationship between two entites. If I save the entity with an indexed list it works fine. But when I update the returned value without changing sth. my ordered index is wrong (null in the db). It doesn't matter if I add getters and setters for the indexed column or not.

Here is my code:

Code:

@Entity
@Table(name = "A_Customer")
public class Customer implements Serializable {

   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue
   private Integer id;

   @NotNull
   private String name;

   @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
   @OrderColumn(name = "orderIndex")
   private List<Order> orders;

   {
      orders = new ArrayList<Order>();
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public List<Order> getOrders() {
      return orders;
   }

   /**
    * Set a new order list and, because of bi-directional OneToMany
    * relationship, this Customer will be set as the Customer of the specified
    * Order.
    *
    * @param orders
    *            the order list to set
    */
   public void setOrders(List<Order> orders) {
      for (Order order : orders) {
         order.setCustomer(this);
      }
      this.orders = orders;
   }

   /**
    * adds a Order to the order list, and, because of bi-directional OneToMany
    * relationship, this Customer will be set as the Customer of the specified
    * Order.
    *
    * @param order
    *            the Order to add
    * @return <code>true</code> if this set did not already contain the
    *         specified element
    */
   public boolean addOrder(Order order) {
      boolean added = false;

      added = orders.add(order);

      if (added) {
         order.setCustomer(this);
      }

      return added;
   }

   /**
    * adds a Order to the order list, and, because of bi-directional OneToMany
    * relationship, this Customer will be set as the Customer of the specified
    * Order.
    *
    * @param index
    *            index at which the specified element is to be inserted
    * @param order
    *            the Order to add
    *
    */
   public void addOrder(int index, Order order) {
      orders.add(index, order);
      order.setCustomer(this);
   }

   /**
    * Removes the Order from this Customer order list based on the Order's id,
    * and, because of a bi-directional OneToMany relationship, this Customer
    * will be removed from the specified Order.
    *
    * @param order
    *            the Order to be removed
    * @return <code>true</code> if this set contained the element with the same
    *         id as the specified Order
    */
   public boolean removeOrder(Order order) {
      boolean removed = false;

      removed = orders.remove(order);

      if (removed) {
         order.setCustomer(null);
      }

      return removed;
   }

}



Code:

@Entity
@Table(name = "A_Order")
public class Order implements Serializable {

   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue
   private Integer id;

   private String number;

   @NotNull
   @ManyToOne
   private Customer customer;

   /**
    * The index column is mapped as a property in the associated entity.
    * Otherwise Hibernate cannot handle indexed lists.
    */
   @Column(name = "orderIndex")
   private Integer orderIndex;

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getNumber() {
      return number;
   }

   public void setNumber(String number) {
      this.number = number;
   }

   public Customer getCustomer() {
      return customer;
   }

   public void setCustomer(Customer customer) {
      this.customer = customer;
   }

}




And my Test:

Code:
/**
    * Saves a customer with orders. Afterwards update the returned customer.
    */
   public static void testSaveAndUpdateCustomerWithOrders() {
      // Initialize orders
      List<Order> orders = generateOrderList();

      // Initialize customer
      Customer customer = new Customer();
      customer.setName("Test");
      customer.setOrders(orders);

      // Save customer
      Customer customerSaved = service.saveOrUpdate(customer);
      Integer id = customerSaved.getId();
      Assert.assertNotNull(id);

      // update customer again
      customerSaved = service.saveOrUpdate(customerSaved);
      id = customerSaved.getId();
      Assert.assertNotNull(id);
   }

/**
    * Generate an order list with 3 entries.
    *
    * @return a new order list
    */
   private static final List<Order> generateOrderList() {
      Order order = new Order();
      order.setNumber("1 Init");
      Order orderSec = new Order();
      orderSec.setNumber("2 Init");
      Order orderThird = new Order();
      orderThird.setNumber("3 Init");

      List<Order> orders = new ArrayList<Order>();
      orders.add(order);
      orders.add(orderSec);
      orders.add(orderThird);
      return orders;
   }


Why is my list indexed lost after updating the same entity? After save the index is right (I checked the db). It's like the example in the Hibernate documentation:
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype-indexbidir
Any help?

Update: If I use the 2 example (JoinColumn instead mappedby - see link) it also works fine when I save and afterwards update the entity. So maybe this is a bug?

I alos find this Issue in Jira:
https://hibernate.atlassian.net/browse/HHH-8083

But I'm using hibernate 4.2.2


Top
 Profile  
 
 Post subject: Re: OrderColumn index is null after updating the entity
PostPosted: Thu Mar 20, 2014 12:41 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
I'm also still having problems with this.

Code:
public class Course{
        @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY, mappedBy="course",orphanRemoval=false)
   @OrderColumn(name="myOrder")
   public List<Module> getModules() {
      return modules;
   }
}

public Module{
   @ManyToOne(fetch=FetchType.LAZY,optional=false)
   @JoinColumn(name="FK_CourseId", nullable=false, updatable=false)
   @NotNull
   public Course getCourse() {
      return course;
   }
}


Code:
Module module = mgr.save(module);
course.getModules().add(module);
mgr.save(course);


Code:
Hibernate: insert into Module (active, FK_CourseId, description, dteCreate, dteUpdate, label, myOrder, objectives, preview, tasks) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update Module set myOrder=? where moduleId=?


But each Module inserted like this has myOrder value of 0. I don't understand why.

Marc


Top
 Profile  
 
 Post subject: Re: OrderColumn index is null after updating the entity
PostPosted: Mon Mar 24, 2014 11:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 18, 2012 5:03 am
Posts: 36
Location: Fort Wayne, Indiana, USA
Thanks guys -- I'll take a look at https://hibernate.atlassian.net/browse/HHH-9078 this morning.


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

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.