@IndexColumn doesn't store the List index
-----------------------------------------
Key: HHH-3425
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3425
Project: Hibernate3
Issue Type: Bug
Components: core
Affects Versions: 3.2.6
Environment: Hibernate: 3.2.6.ga, hibernate-annotations-3.3.0.GA, MySQL:
5.0.51b-community-nt
Reporter: J
Priority: Minor
When you have a Parent class with a bidirectional one-to-many relation to a Child class
(as a List), and the index column is mapped as a property in the associated entity,
Hibernate will not store the index position of the List in the db. The value will stay 0
or null (depending whether you use int of Integer as index type).
Example taken directly from 2.4.6.2.3:
http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.ht...
With small change, for example "order" is changed to "position",
because "order" is not allowed as columnname in MySQL.
------------ PARENT --------------
package entities;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Parent {
// Properties
@Id @GeneratedValue
private Long id;
@OneToMany(mappedBy="parent")
@org.hibernate.annotations.IndexColumn(name="position")
private List<Child> children = new ArrayList<Child>();
private String name;
// Getters & Setters
public List<Child> getChildren() { return children; }
public void setChildren(List<Child> children) { this.children = children; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Parent() {}
}
------------------
----- CHILD -----
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.hibernate.Session;
import persistence.HibernateUtil;
@Entity
public class Child {
// Properties
@Id @GeneratedValue
private Long id;
@Column(name="position")
private int position;
@ManyToOne
@JoinColumn(name="parent_id", nullable=false)
private Parent parent;
private String name;
// Getters & Setters
public Parent getParent() { return parent; }
public void setParent(Parent parent) { this.parent = parent; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
------------------------------------
------ TEST --------------
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Parent parent = new Parent();
Child child1 = new Child();
parent.getChildren().add(child1);
child1.setParent(parent);
Child child2 = new Child();
parent.getChildren().add(child2);
child2.setParent(parent);
session.save(parent);
session.save(child1);
session.save(child2);
session.getTransaction().commit();
--------------------------
After running this, the DB will stay:
mysql> select * from child;
+----+------+----------+-----------+
| id | name | position | parent_id |
+----+------+----------+-----------+
| 1 | NULL | NULL | 1 |
| 2 | NULL | NULL | 1 |
+----+------+----------+-----------+
instead of:
mysql> select * from child;
+----+------+----------+-----------+
| id | name | position | parent_id |
+----+------+----------+-----------+
| 1 | NULL | 1 | 1 |
| 2 | NULL | 2 | 1 |
+----+------+----------+-----------+
See also:
http://forum.hibernate.org/viewtopic.php?p=2392563
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira