2014-12-22 30 views
1

我有使用休眠映射的问题。 两个表矩阵和matrix_value。 enter image description here休眠不通过生成的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> 

什么是错? 在此先感谢。

+0

由于错误处理约束(仅驻留在MATRIX_VALUES中),因此似乎在调用saveOrUpdate时未填充“matrix_id”。你看过什么是创建'MatrixValue []值'并确保它被填充它吗?如果是这样,请添加该代码。 – Ascalonian

+0

感谢您的回答。那么,我敢肯定,** MatrixValue []值**已填充。它显示在我的jsp上。每个'值'都有matrix_id = 0。并且插入后必须有'matrix_id'符合Matrix实体的id。在jdbc中,它是在get_generated_id的帮助下完成的。在冬眠我已经添加了这个属性。 – Devour

+0

对不起,我的意思是任何正在创建'值',它是填充'matrixId'字段。奇怪的是,它始终是相同的值 – Ascalonian

回答

0

删除MatrixValues类中的@Transient和matrix_id字段(带注释)。你也不需要。

说明:

  1. 的matrixValues集合不是短暂的。它应该被持久化,尽管Matrix并不是该关联的'拥有者'(当持久化Hibernate会查看映射字段以查找要存储的值时,虽然这应该产生相同的值),但这称为'反向'侧(双向)关联。
  2. matrixValues中的matrix_id列将填充分配给矩阵字段的矩阵的id。添加额外的字段是多余的,并注释它为@PrimaryKeyJoinColumn只是错误的:该列不是包含它的表的主键的一部分。
+0

如果我将删除MatrixValues类中的matrix_id字段,我将如何从** matrix_values **表中获取值?现在我有方法** MatrixValue [] getValuesById(int matrixId)**其中**值= session.createQuery(“从MatrixValue值其中values.matrixId =”+ matrixId).list(); **没有matrix_id它doesn'工作。那我该如何注释字段? Soory,我是休眠中的新人。 – Devour

+0

好吧,矩阵中矩阵值的集合被急切地加载,所以你可以通过它的id来获取矩阵,并通过访问集合来获取值。 –

+0

是的,我试过了。但不是到处都是这种方法是合适的。 – Devour