For complicated reasons, I have a view defined in my database and I want hibernate to be able to read this view, so I mapped it just like it was a regular table. This all seems to be working fine in production.
Now I’m running a test and it’s blowing up. I turned the hibernate show sql and sql comments on and here’s what I found:
Hibernate: /* FROM PostponeDateLimit WHERE postponeDate>=? AND postponeDate <=? AND controlCode = ? ORDER BY postponeDate ASC */ select postponeda0_.REQUEST_DATE as REQUEST1_5_, postponeda0_.COURTLOCATION as COURTLOC2_5_, postponeda0_.ALLOWED as ALLOWED5_, postponeda0_.POSTPONED as POSTPONED5_, postponeda0_.DATECAP as DATECAP5_ from POSTPONE_DATE_VIEW postponeda0_ where postponeda0_.REQUEST_DATE>=? and postponeda0_.REQUEST_DATE<=? and postponeda0_.COURTLOCATION=? order by postponeda0_.REQUEST_DATE ASC
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
Hibernate: /* update com.acs.gs.juror.pojo.PostponeDateLimit */ update POSTPONE_DATE_VIEW set ALLOWED=?, POSTPONED=?, DATECAP=? where REQUEST_DATE=? and COURTLOCATION=?
2012-07-18 16:07:44,503 35529 WARN org.hibernate.util.JDBCExceptionReporter | SQL Error: 4406, SQLState: S1000
2012-07-18 16:07:44,503 35529 ERROR org.hibernate.util.JDBCExceptionReporter | Update or insert of view or function 'EJUROR_POSTPONE_DATE_VIEW' failed because it contains a derived or constant field.
So updating the view is causing the exception.
Only the first query is being initiated by me. The updates appear to be created by hibernate for some strange reason. I can’t for the life of me figure out why and why it’s doing it on my local test box and not on our live box.
Here’s my hibernate mapping:
<class name="com.pojo.PostponeDateLimit" table="POSTPONE_DATE_VIEW">
<composite-id class="com.acs.gs.juror.pojo.PostponeDateLimitKey" mapped="true">
<key-property name="postponeDate" type="calendar" column="REQUEST_DATE"/>
<key-property name="controlCode" column="COURTLOCATION"/>
</composite-id>
<property name="maxCount" column="ALLOWED"/>
<property name="postponedCount" column="POSTPONED"/>
<property name="dateCap" column="DATECAP"/>
</class>
Does anyone have an insight at all?
You could make the Entity read-only as per Hibernate documents
In this case probably best to make the com.pojo.PostponeDateLimit class immutable. This will also catch any code that tries to change the data – which I suspect is what is happening not Hibernate doing something