2012-10-25 152 views
1

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

初始化songsList空白/空單,如:

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

我不知道這是否會解決這一問題或沒有。如果不是,那麼你可以分享保存的詳細代碼ContentPack以及你如何設置ContentItem在裏面。

+0

所有這些都有getter和setter。所以他們工作得很好。問題來自Hibernate方面。我需要找到註釋它們的正確方法 – dinesh707

+1

可以嘗試不加註釋。你會參考[鏈接](http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html),它包含很好的簡單例子,任何人都可以明白它。 –

相關問題