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