2012-09-18 74 views
1

全部。我有以下代碼。Hibernate集和多對多索引列問題

@ManyToMany(targetEntity = com.acs.gs.juror.model.security.SecurityGroup.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @JoinTable(name = "REP_DSGN_SEC_GRP_LNK", 
     joinColumns = {@JoinColumn(name = "REPORT_DESIGN_UUID", referencedColumnName = "UUID")}, 
     inverseJoinColumns = {@JoinColumn(name = "SECURITY_GROUP_UUID", referencedColumnName = "UUID")}) 
    @IndexColumn(name="HIBERNATE_IDX") 
    private List<SecurityGroup> groups; 

哪一天開始有多個包問題。所以我固定它通過將其更改爲一組,即:

@ManyToMany(targetEntity = com.acs.gs.juror.model.security.SecurityGroup.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @JoinTable(name = "REP_DSGN_SEC_GRP_LNK", 
     joinColumns = {@JoinColumn(name = "REPORT_DESIGN_UUID", referencedColumnName = "UUID")}, 
     inverseJoinColumns = {@JoinColumn(name = "SECURITY_GROUP_UUID", referencedColumnName = "UUID")}) 
    @IndexColumn(name="HIBERNATE_IDX") 
    private Set<SecurityGroup> groups; 

,直到我跑我的數據加載,並得到下面的錯誤也能正常工作:

[java] Exception in thread "main" com.acs.gs.juror.dao.FailedInsertException: Unable to save object:ALTF Funds By Case|ALTF Funds By Case Report|java.util.GregorianCalendar[time=18000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1970,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-18000000,DST_OFFSET=0]|null|ALTFFundsByCase||PDF|[]|Lexptrain|Lexptrain|314||REPORTING|OUTPUT_TYPE|Browser|Browser|Jan 1, 1970 12:00:00 AM||null|null|null|null|null|null|null|null|null|null|null| 
[java] at com.acs.gs.juror.dao.hibernate.HibernateDAO.create(HibernateDAO.java:65) 
[java] at com.acs.gs.juror.dataload.cs.maricopa.LoadReportDesigns.loadData(LoadReportDesigns.java:116) 
[java] at com.acs.gs.juror.dataload.cs.maricopa.SeedDataLoader.loadData(SeedDataLoader.java:48) 
[java] at com.acs.gs.juror.dataload.cs.maricopa.SeedDataLoader.main(SeedDataLoader.java:18) 
[java] Caused by: org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 
[java] at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636) 
[java] at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) 
[java] at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424) 
[java] at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
[java] at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694) 
[java] at com.acs.gs.juror.dao.hibernate.HibernateDAO.create(HibernateDAO.java:62) 
[java] ... 3 more 
[java] Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) 
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
[java] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) 
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) 
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171) 
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) 
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027) 
[java] at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390) 
[java] at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420) 
[java] ... 6 more 
[java] Caused by: java.sql.BatchUpdateException: Cannot insert the value NULL into column 'HIBERNATE_IDX', table 'agilejury-thehl_MC.dbo.REP_DSGN_SEC_GRP_LNK'; column does not allow nulls. INSERT fails. 
[java] at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947) 
[java] at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723) 
[java] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
[java] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) 
[java] ... 13 more 

我試圖刪除@IndexColumn進入無濟於事。 Hibernate無論如何創建列。如果hibernate在沒有映射它的情況下創建它,我沒有辦法在其中存儲一個值,那它怎麼會有空問題呢?

我有點卡在這一點,不知道如何進行。

謝謝。

+0

你確定在持久化的時候你已經在'SecurityGroup'中初始化了'HIBERNATE_IDX'的字段 – SiB

回答

0

看起來這是另一個Hibernate黑盒問題,沒有人真正理解。我最終創建了自己的實體,而不是一個穩定的連接,並解決了問題。

0

向我們展示REP_DSGN_SEC_GRP_LNK表的創建表語句,我將向您顯示您的空限制在哪裏。取出空限制,它不會抱怨它是空的。缺點是你會將空值填入不應該爲空的東西。

您的List不允許使用空值,這可以保護您免受此問題的困擾。您將其更改爲Set,它允許使用空值。所以有幾個選項:

  1. 修正你的數據在你的設置沒有空值。
  2. 解除對空值的限制。
1

由於錯誤清楚地表明。

Caused by: java.sql.BatchUpdateException: Cannot insert the value NULL into column 'HIBERNATE_IDX', table 'agilejury-thehl_MC.dbo.REP_DSGN_SEC_GRP_LNK'; column does not allow nulls. INSERT fails.

它說你的groups收集SecurityGroup類型的元素之一具有HIBERNATE_IDX場定義爲null。當元素被添加到groups時,您應該查找此元素的初始化。