开发者

OrderedColumn in combination playframework gives me for all entries 0 as index

I have the following model (playframework):

@Entity
public class Album extend开发者_运维问答s Model{

    public Album(){
        this.creationDate = new Date();
    }

    public Album(String name){
        this.creationDate = new Date();
        this.name = name;
    }

    @Required
    @Column(unique = true)
    public String name;
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date creationDate;
    @OneToMany(cascade=CascadeType.ALL, mappedBy="album")
    @OrderColumn(name="position")
    public List<Image> images = new ArrayList<Image>();    
}

and:

@Entity
public class Image extends Model {

    public Image(String description) {
        this.description= description;
    }
    @Required
    public String description;
    @Required
    @ManyToOne
    @JoinColumn(name="album_id", nullable=false)
    public Album album;
    @Column(name="position")
    public int position;

    public void addAlbum(Album album){
        this.album = album;
        album.images.add(this);
    }
}

In my tests I do the following:

@Test
public void indexColumn(){
    Album album = new Album("newalbum");
    Image image1 = new Image("newimage1");
    Image image2 = new Image("newimage2");
    image1.addAlbum(album);
    image2.addAlbum(album);
    album.save();

}

When I look at the DB, I can see that position is 0 for both images. I think I did exactly that what's in the hibernate doc Hibernate Documentation, but it is not working. So I am wondering if this is not working in combination with play framework.

Any Ideas?

BR, Rene


The one difference between what you have, and what the Hibernate document is showing, is that you don't user the @JoinColumn annotation. I know you specifically mention the join in the @OneToMany annotation parameters, but this is the only specific difference I can see.

The documentation says

@OneToMany
@OrderColumn(name="order")
@JoinColumn(name="parent_id", nullable=false)
private List<Child> children;

So maybe your code should read

@OneToMany(cascade=CascadeType.ALL)
@OrderColumn(name="position")
@JoinColumn(name="album_id", nullable=false)
public List<Image> images = new ArrayList<Image>(); 

I don't think Play would act differently as it just uses Hibernate under the hood to perform what you are trying to do. The only other thing I can think of, is a versioning issue between the version of Hibernate documentation and the version shipped with Play.


I finally found a working solution here:

@Entity
public class Album extends Model{

    public Album(){
        this.creationDate = new Date();
    }

    public Album(String name){
        this.creationDate = new Date();
        this.name = name;
    }

    @Required
    @Column(unique = true)
    public String name;
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date creationDate;
    @OneToMany(cascade=CascadeType.ALL, mappedBy="album")
    @OrderColumn(name="position_index")
    public List<Image> images = new ArrayList<Image>();    
}

and

@Entity
public class Image extends Model {

    public Image(String description) {
        this.description= description;
    }
    @Required
    public String description;
    @Required
    @ManyToOne
    public Album album;

    public void addAlbum(Album album){
        this.album = album;
        album.images.add(this);
    }
}

With that mapping Hibernate creates a new Table: album_image which contains album_id,images_id and position_index, this time updated corresponding to the values in the list.


I don't see you setting the value of position at any point. Setting position as the field to order by only means that the list will be ordered according to the value of that field, but you will need to set that value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜