复合键

时间:2017-08-29 11:37:13

标签: java hibernate orm

我的实体session.merge()

时,composite key方法存在问题

在这里,我有一个 composite-id 的实体。因此,当我尝试使用session.save()方法时,它会起作用,但当我尝试使用session.merge()时,会出现以下错误。

我已经尝试了两种不同版本的Hibernate

  • hibernate-core.3.6.7.Final
  • hibernate-core.4.3.11.Final

错误

   org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:50)
        at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76)
        at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:273)
        at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:93)
        at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162)
        at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1189)
        ... 26 more
    Caused by: java.sql.BatchUpdateException: Column 'proName' cannot be null
        at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1809)
        at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1271)
        at com.mysql.jdbc.jdbc2.optional.StatementWrapper.executeBatch(StatementWrapper.java:648)
        at org.jboss.jca.adapters.jdbc.CachedPreparedStatement.executeBatch(CachedPreparedStatement.java:708)
        at org.jboss.jca.adapters.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:1077)
        at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:127)
        ... 42 more
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'KEY_' cannot be null
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
        at com.mysql.jdbc.Util.getInstance(Util.java:383)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
        at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1767)
        ... 47 more

我已在 proName 字段中提供了该值。

我搜索了与此相关的内容,我发现了jira问题,他们已经提到它已经在 3.2.0.cr4 版本中修复了。 的 https://hibernate.atlassian.net/browse/HHH-1982

以下是我的代码段。

Product.xml

<hibernate-mapping>
   <class name="com.asite.stock.model.Product" table="products">

       <composite-id>
        <key-property name="productId" column="pid"  />
    <key-property name="proName" type="java.lang.String" column="proName"/>
        </composite-id>

        <property name="price" column="price" length="10" />

   </class> 
</hibernate-mapping>

Product.java

public class Product implements java.io.Serializable{

        private static final long serialVersionUID = 1L;

        private int productId;
        private String proName;
        private double price;
        // getter setter
    }

Main.java

public class App {

   public static void main(String[] args) {

       ApplicationContext appContext =
                new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml");

       ProductBo productBo = (ProductBo) appContext.getBean("productBo");

       Product product = new Product();
        product.setProductId(1021);
        product.setProName("iPhone111");
        product.setPrice(25000);
        productBo.merge(product);

       System.out.println("Done");
    }
}

productBo.merge()方法将调用 session.merge()方法。

你能帮我解决一下吗?

0 个答案:

没有答案