Thom
Thom

Reputation: 15052

Hibernate sets and many to many index column issue

all. I had the following code.

  @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;  

Which one day started having a multiple bag issue. So I fixed it by changing it to a set, thus:

  @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;  

This works fine until I run my data loader and get the following error:

[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

I have tried deleting the @IndexColumn entry to no avail. Hibernate creates the column regardless. If hibernate creates it without me mapping it, I don't have a way to store a value in it, so how can it be having a problem with it being null?

I'm kinda stuck at this point and have no idea how to proceed.

Thanks.

Upvotes: 1

Views: 1921

Answers (3)

Thom
Thom

Reputation: 15052

It looks like this is another Hibernate black box issue that no one really understands. I ended up just creating my own entity instead of a join stable and that fixed the issue.

Upvotes: 0

Bharat Sinha
Bharat Sinha

Reputation: 14363

As the error clearly indicates .

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.

Which says one of your elements of SecurityGroup type in the groups collection have HIBERNATE_IDX field defined as null. You should look for initialization of this element when an element is added to the groups.

Upvotes: 1

Eric Leschinski
Eric Leschinski

Reputation: 154063

Show us the create table statement for the REP_DSGN_SEC_GRP_LNK table and I will show you where your null restriction is. Take out the null restriction and it won't complain about it being a null. The drawback is you will be cramming nulls into something that shouldn't be null.

Your List does not allow nulls, which is protecting you from this problem. You changed it to Set, which allows nulls. So a few options:

  1. Fix your data in your Set to not have nulls.
  2. Loosen the restriction on nulls.

Upvotes: 0

Related Questions