I have the following model (playframework):
@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")
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
I finally found a working solution here:
and
With that mapping Hibernate creates a new Table:
album_imagewhich containsalbum_id,images_idandposition_index, this time updated corresponding to the values in the list.