【发布时间】:2011-04-04 22:36:28
【问题描述】:
我目前正在尝试使用 @OneToMany(cascade=CascadeType.ALL) 为简单的对象列表保留一个集合。 Parent_Child 的表在 MySQL 中创建,但每个对象的键在使用 SaveOrUpdate 时不会更新。知道问题是什么吗? (定义了我的父键并生成了子键)。在坚持使用 saveOrUpdate 之前,我将子对象添加到父对象的集合中。我正在使用带有 hibernate 3 的 MySQL,并且我的 auto 属性设置为 create-drop。
测试类:
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;
...
}
【问题讨论】:
-
实际代码不起作用将非常有助于缩小几十个潜在原因。