我有一个 A 类和 B 类,如下所示。我遇到的问题是,在我的数据库中查找映射表具有三列“A_id”、“setOne_id”和“setTwo_id”。
“A_id”和“setOne_id”也是主键。
public class A implements Serializable{
public A(){
setOne = new HashSet<B>();
setTwo = new HashSet<B>();
}
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column
private long id;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@ElementCollection(targetClass=B.class)
@Column(nullable=true,columnDefinition="bigint(20) default NULL")
private Set<B> setOne;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@ElementCollection(targetClass=B.class)
@Column(nullable=true,columnDefinition="bigint(20) default NULL")
private Set<B> setTwo;
// Getter and Setter
}
public class B implements Serializeable{
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column
private long id;
private String data;
// Getter and Setter
}
尝试在 setOne 或 setTwo 中保存带有添加 B 对象的 A 对象最终会出现以下异常:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1256)
at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:292)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at com.cgi.liebichs.bachelorarbeit.server.App.main(App.java:131)
Caused by: java.sql.SQLException: Field 'setTwo_id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
... 13 more
我怎样才能使这件事起作用?