如何插入一个包含对象列表的对象?

时间:2012-10-25 08:51:33

标签: hibernate

ContentPack类,

@Entity
public class ContentPack {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Property
private String name;

@OneToMany  // ????
private List<ContentItem> songsList;
}

接下来是ContentItem类,

@Entity
public class ContentItem {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String mp3Url;
}

但是当我尝试拨打以下内容时,

session.beginTransaction();
session.save(contentPack);
session.getTransaction().commit();

我收到以下错误。我相信list会产生错误。我该如何解决?

错误:

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    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)

1 个答案:

答案 0 :(得分:1)

使用空白/空列表初始化songsList,如:

private List<ContentItem> songsList = new ArrayList<ContentItem>();

我不确定这是否能解决问题。如果没有,那么您可以分享保存 ContentPack 的详细代码以及如何在其中设置 ContentItem