我有使用休眠映射的問題。 兩個表矩陣和matrix_value。 休眠不通過生成的ID
CREATE TABLE matrix (
matrix_id INT AUTO_INCREMENT,
number_of_rows INT NOT NULL DEFAULT 0,
number_of_cols INT NOT NULL DEFAULT 0,
PRIMARY KEY (matrix_id)
) ENGINE =InnoDB;
CREATE TABLE matrix_value (
id INT AUTO_INCREMENT,
matrix_id INT NOT NULL DEFAULT 0,
row_id INT NOT NULL DEFAULT 0,
column_id INT NOT NULL DEFAULT 0,
matrix_val INT NOT NULL DEFAULT 0,
PRIMARY KEY (id),
CONSTRAINT FK_matrix_value_matrix
FOREIGN KEY (matrix_id) REFERENCES matrix (matrix_id)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE =InnoDB;
matrix_values的生成和插入矩陣行和它 必須自動採取matrix_id並把它傳遞給matrix_values列之後,但它拋出
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'matrix_id' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
此外其添加到表'矩陣'附加列'matrixValues'填充null。
這裏是方法
public void fillMatrixWithValues(MatrixValue[] values) throws Exception {
Session session = HibernateUtils.openSession();
Transaction tx = null;
try {
tx = session.getTransaction();
tx.begin();
if (values != null && values.length != 0) {
for (int i = 0; i < values.length; i++) {
session.saveOrUpdate(values[i]);
}
tx.commit();
}
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
logger.error("Error fillMatrixWithValues-method: " + e.getMessage());
throw e;
} finally {
session.close();
}
}
矩陣(POJO)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "matrix_id", unique = true, nullable = false)
private int matrixId;
@Column(name = "number_of_rows", unique = false, nullable = false)
private int numberOfRows;
@Column(name = "number_of_cols", unique = false, nullable = false)
private int numberOfCols;
@OneToMany (mappedBy= "matrix", fetch = FetchType.EAGER)
@Transient
private List<MatrixValue> values;
MatrixValues(POJO)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "id", unique = true, nullable = false)
private int id;
@PrimaryKeyJoinColumn
@Column(name = "matrix_id", unique = false, nullable = false, insertable = false, updatable = false)
private int matrixId;
@Column(name = "row_id", unique = false, nullable = false)
private int rowId;
@Column(name = "column_id", unique = false, nullable = false)
private int columnId;
@Column(name = "matrix_val", unique = false, nullable = false)
private int value;
@ManyToOne
@JoinColumn(name = "matrix_id")
private Matrix matrix;
getters and setters
ħ ibernate.xml
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/matrixbase</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.jdbc.batch_size">30</property>
<property name="hibernate.jdbc.batch_versioned_data">true</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<mapping class="kuidin.task5.entity.Matrix" />
<mapping class="kuidin.task5.entity.MatrixValue" />
<hibernate-configuration>
什麼是錯? 在此先感謝。
由於錯誤處理約束(僅駐留在MATRIX_VALUES中),因此似乎在調用saveOrUpdate時未填充「matrix_id」。你看過什麼是創建'MatrixValue []值'並確保它被填充它嗎?如果是這樣,請添加該代碼。 – Ascalonian
感謝您的回答。那麼,我敢肯定,** MatrixValue []值**已填充。它顯示在我的jsp上。每個'值'都有matrix_id = 0。並且插入後必須有'matrix_id'符合Matrix實體的id。在jdbc中,它是在get_generated_id的幫助下完成的。在冬眠我已經添加了這個屬性。 – Devour
對不起,我的意思是任何正在創建'值',它是填充'matrixId'字段。奇怪的是,它始終是相同的值 – Ascalonian