dinesh707
dinesh707

Reputation: 12592

How to insert a object which has a list of objects in it?

ContentPack class,

@Entity
public class ContentPack {

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

@Property
private String name;

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

Next the ContentItem class,

@Entity
public class ContentItem {

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

    private String mp3Url;
}

but when i try to call the following,

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

i get the following error. I believe the list creates the error. How can i fix it?

Error :

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)

Upvotes: 1

Views: 2419

Answers (1)

kamlesh patel
kamlesh patel

Reputation: 385

Initialize songsList with blank/empty list , like :

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

I am not sure whether this will solve the issue or not . if not then can you share detail code for the saving ContentPack and how you are setting ContentItem in it.

Upvotes: 1

Related Questions