开发者

Play Framework and the use of collections in model classes

开发者 https://www.devze.com 2023-03-27 10:45 出处:网络
I\'ve just started with the Play Framework and I\'m running into some problems using a collection in one of my model classes. I am very new to Play and JPA/Hibernate, so please excuse my noobness... I

I've just started with the Play Framework and I'm running into some problems using a collection in one of my model classes. I am very new to Play and JPA/Hibernate, so please excuse my noobness... I've tried searching on the web for answers but couldn't find what I was looking for.

Essentially, I have 2 models:

@Entity
public class Song extends Model
{
    @Column(unique = true)
    public int songId;
    public String name;
    public String artist;

    public Song() {}

    public Song(int songId, String name, String artist)
    {
        this.songId = songId;
        this.name = name;
        this.artist = artist;
    }
}

@Entity
public class CurrentSongList extends Model {

    @OneToMany(orphanRemoval=false)
    public List<Song> currentSongList;
}

What I want here is to have all songs in the database and then a list to hold a subset of these songs temporarily (the list contents will change over time)... if the list is deleted, I don't want the songs to be deleted (the songs shouldn't hold any reference to the list). All I then try to do on application startup is load songs from a data file and insert a subset of songs into the list and save the list with the code below.... this is where I have had endless problems.

Fixtures.deleteDatabase();
Fixtures.loadModels("songlist.yaml");

List<Song> songs开发者_开发知识库 = Song.findAll();
CurrentSongList.deleteAll();
CurrentSongList currentSongs = new CurrentSongList();
currentSongs.currentSongList = new ArrayList<Song>();
currentSongs.currentSongList.add(songs.get(0));
currentSongs.currentSongList.add(songs.get(1));

EntityManager em = JPA.em();
em.persist(currentSongs);
em.flush();

If I leave out the flush call, the list doesn't appear to be saved when fetching its contents from the DB later on. The flush call however results in the following exception:

Caused by: org.hibernate.HibernateException: Found two representations of same collection: models.CurrentSongList.currentSongList
    at org.hibernate.engine.Collections.processReachableCollection(Collections.java:175)
    at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:60)
    at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:122)
    at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:83)
    at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:77)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:165)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:240)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)

I realize I am probably doing something silly here (is the flush() after the persist really necessary?) and don't understand how this stuff works properly, however, I've struggled to find information on this exact problem. Any help on how I should go about achieving the above will be greatly appreciated.

Thanks!


With the Play Framework, you don't need to call persist. Try calling

currentSongs.save();

Also, the Play Framework will automatically create an id for your Song Model. Thus you may not need the songId unlessit is some predefined value you want to keep.

0

精彩评论

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