views:

39

answers:

1

I currently am trying to persist a collection using @OneToMany(cascade=CascadeType.ALL) for a simple list of objects. The table for Parent_Child gets created in MySQL but the keys for each object are not updated upon using SaveOrUpdate. Any idea what the issue is? (My parent key is defined and the children are generated). I add the children to the parent object's collection before persisting with saveOrUpdate. I'm using MySQL with hibernate 3 and my auto property is set to create-drop.

The test class:

public class Tester {
    public static void main(String[] args) {
        VideoChannel testChannel = new VideoChannel("Test Channel");
        VideoChannelMap v = new VideoChannelMap(testChannel, "Test Map");
        VideoSource sc2Vid = new VideoSource("starcraft-ii-ghost-of-the-past.mp4", "EinghersStreamingBucket");
        testChannel.add(sc2Vid);
        Session s = HibernateSessionFactory.getSession();
        s.beginTransaction();
        s.saveOrUpdate(v);
        s.close();
    }           
}

The entities:

@Entity
public class VideoChannelMap {
    @Id 
    String name;

    @OneToMany(cascade=CascadeType.ALL)
    List<VideoChannel> channelMap;

    public VideoChannelMap(VideoChannel initialVid, String name)
    {
        this.name = name;
        channelMap = new ArrayList<VideoChannel>();
        channelMap.add(initialVid);
        initialVid.setParent(this);
    }
}

@Entity
public class VideoChannel {

    @Id @GeneratedValue
    Long id;
    ...
}
+2  A: 

You have to actually commit your transaction. The behavior when you close a session with a transaction still open isn't very well defined and will likely depend on how your database is set up underneath.

Transaction t = s.beginTransaction();
s.saveOrUpdate(v);
t.commit();
s.close();

Obviously you should also have some try-catch-finally action going on in there for "real" code ;)

Affe
Committing the transaction should definitely help indeed.
Pascal Thivent