-->
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.  [ 4 posts ] 
Author Message
 Post subject: IndexColumn with ArrayList doesn't work
PostPosted: Mon Jul 28, 2008 9:17 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
@IndexColumn with ArrayList doesn't work

MySQL: 5.0.51b-community-nt
Hibernate: 3.2.6.ga, hibernate-annotations-3.3.0.GA


Somehow the position column (the index of the ArrayList) isn't used by Hibernate. Can somebody tell me what's going wrong?

This code is taken from http://www.hibernate.org/hib_docs/annot ... indexbidir
But with small adjustments, for example "order" is changed to "position", because "order" is not allowed as columnname in MySQL.

Code:
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() {}
}


Code:
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; }

   
    public static void main(String[] args) {
       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 main method, the position column will stay at the integer default 0. Even after running this serveral times.

Bug?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 5:49 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
This still isn't working.

The result is
Code:
mysql> select * from child;
+----+------+----------+-----------+
| id | name | position | parent_id |
+----+------+----------+-----------+
|  1 | NULL |     NULL |         1 |
|  2 | NULL |     NULL |         1 |
+----+------+----------+-----------+


instead of:
Code:
mysql> select * from child;
+----+------+----------+-----------+
| id | name | position | parent_id |
+----+------+----------+-----------+
|  1 | NULL |        1 |         1 |
|  2 | NULL |        2 |         1 |
+----+------+----------+-----------+




-Working: IndexColumn does work for value type collections
-Working: IndexColumn does work for entities where the index column is NOT mapped as a property in the associated class. (which is the second example of 2.4.6.2.3.).
-Working: Populating Arraylist from a DB works for entities where the index column IS mapped as a property in the associated class. (which is the first example of 2.4.6.2.3.). So when you have the positions already set in the db, Hibernate will populate the ArrayList in the correct order.
-NOT Working: Updating/setting the IndexColum in the DB does NOT work for entities where the index column IS mapped as a property in the associated class. (which is the first example of 2.4.6.2.3.). The index column will stay 0 or null, instead of being the index of the arraylist.

Can anybody clarify this?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 7:29 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
Created a bug report for it:
http://opensource.atlassian.com/project ... e/HHH-3425


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 1:05 pm 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
Solved. It is not a bug.

I was using this on the inverse side:
parent.getChildren().add(child1);

which is wrong. I should have used child1.addParent(parent) instead. Then the position will be updated.

More info, see
http://opensource.atlassian.com/project ... se/ANN-632


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.