2010-08-27 60 views
0

我目前正在嘗試使用@OneToMany(cascade = CascadeType.ALL)爲對象的簡單列表持久化集合。 Parent_Child的表在MySQL中創建,但每個對象的鍵在使用SaveOrUpdate時不會更新。任何想法是什麼問題? (我的父母的關鍵是定義和孩子生成)。在保存saveOrUpdate之前,我將這些子項添加到父對象的集合中。我使用MySQL與休眠3和我的自動屬性設置爲創建 - 下降。OneToMany帶註釋的集合不會通過Hibernate持久化

測試類:

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(); 
    }   
} 

的實體:

@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; 
    ... 
} 
+0

實際的代碼不工作將​​是縮小了數十非常有幫助潛在的原因。 – Affe 2010-08-27 17:39:36

回答

2

你有實際提交事務。當您關閉一個仍然打開的事務的會話時的行爲不是很好定義,並且可能取決於您的數據庫在下面設置的方式。

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

很明顯,你也應該有一定的try-catch-終於行動了有「真正的」代碼回事;)

+0

承諾交易應該確實有幫助。 – 2010-08-27 23:26:50

相關問題