Okay I'm sure I'm doing it wrong, but I can't figure out what the right way is. Everything I have tried (short of having no object associations) is causing update statements instead of deletes.
I have the following mappings:
Code:
<!-- Product Colour -->
<class name="com.fgl.ina.stylecreation.coloursize.ProductColour" table="product_colour">
<id name="colourID" column="colour_ID" unsaved-value="none" type="integer">
<column name="colour_ID" not-null="true" unique="false"/>
<generator class="assigned"/>
</id>
<many-to-one name="parentProduct" column="ina_prod_id" class="com.fgl.ina.stylecreation.Product"
not-null="true" unique="false"/>
</class>
<!-- Product -->
<class name="com.fgl.ina.stylecreation.Product" table="product_style">
<id name="productID" column="ina_prod_id" type="integer" unsaved-value="0">
<generator class="net.sf.hibernate.id.IdentityGenerator"/>
</id>
<set name="colours" cascade="all-delete-orphan" inverse="false" lazy="true" order-by="colour_ID">
<key column="ina_prod_id"/>
<one-to-many class="com.fgl.ina.stylecreation.coloursize.ProductColour"/>
</set>
</class>
I also tried having the child class like this:
Code:
<!-- Product Colour -->
<class name="com.fgl.ina.stylecreation.coloursize.ProductColour" table="product_colour">
<composite-id unsaved-value="none">
<key-many-to-one name="parentProduct" column="ina_prod_id" class="com.fgl.ina.stylecreation.Product"/>
<key-property name="colourID" column="colour_ID" type="integer"/>
</composite-id>
</class>
and like this:
Code:
<!-- Product Colour -->
<class name="com.fgl.ina.stylecreation.coloursize.ProductColour" table="product_colour">
<composite-id unsaved-value="none">
<key-property name="productID" column="ina_prod_id" type="integer"/>
<key-property name="colourID" column="colour_ID" type="integer"/>
</composite-id>
</class>
I've tried changing the unsaved-value to "any" which didn't seem to have an affect.
When the child class was mapped as composite-id I had Serializable, equals(), and hashCode() as required.
I've tried explicitly calling delete on the instances I want to delete, I've tried letting the cascade="all-delete-orphan" on the parent do it's thing and the same error happens each way:
My debug log looks like this:
Code:
Hibernate: update product_colour set ina_prod_id=null where ina_prod_id=? and colour_ID=?
10:50:43,542 WARN JDBCExceptionReporter:38 - SQL Error: 515, SQLState: HY000
10:50:43,552 ERROR JDBCExceptionReporter:46 - [Macromedia][SQLServer JDBC Driver][SQLServer]Cannot insert the value NULL into column 'ina_prod_id', table 'INA.dbo.product_colour'; column does not allow nulls. UPDATE fails.
10:50:43,552 WARN JDBCExceptionReporter:38 - SQL Error: 515, SQLState: HY000
10:50:43,552 ERROR JDBCExceptionReporter:46 - [Macromedia][SQLServer JDBC Driver][SQLServer]Cannot insert the value NULL into column 'ina_prod_id', table 'INA.dbo.product_colour'; column does not allow nulls. UPDATE fails.
10:50:43,552 ERROR JDBCExceptionReporter:75 - Could not execute JDBC batch update macromedia.jdbc.base.BaseBatchUpdateException: [Macromedia][SQLServer JDBC Driver][SQLServer]Cannot insert the value NULL into column 'ina_prod_id', table 'INA.dbo.product_colour'; column does not allow nulls. UPDATE fails.
at macromedia.jdbc.sqlserver.SQLServerImplStatement.getBatchRowsAffectedCount(Unknown Source)
at macromedia.jdbc.base.BasePreparedStatement.executeBatch(Unknown Source)
at jrun.sql.JRunStatement.executeBatch(JRunStatement.java:421)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:50)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:116)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2324)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2278)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2219)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:56)
...
If I load a List of the child objects manually instead of loading the parent product and getting them that way I seem to be able to delete them just fine, but if I load the parent object and try to delete them or just remove them from the collection and update the parent I get the Update behaviour trying to set a not-null column to null instead of a delete being issued.
What am I doing wrong? What is the correct way to handle this? My ProductColour object is really just a cross reference being used for convenience as an object in a specific scenario.