I have a collection mapping. Recently I tried to make the key not-null because in the database I want both the foreign keys on this table to be not null. But when I try to save an object that would put data into this table I get an exception about having a null value. But nothing should be null. I'm using hibernate 3.2.0 due to bugs in more recent versions.
The key in question is the key column S_ComponentInstanceID, and the column T_ComponentInstanceID.
Note: I just made it null in the metadata, I have not regenerated the db yet to get the not-null constraing put onto the actual db. But this exception is on hibernate level not jdbc driver.
This could be a bug as I have the same type of mapping using a list and it works. But this mapping is set not list, and the object in question must be in two separate sets. And its failing.
This is the class file.
Code:
public final class DBComponentInstance extends DBContainableGraphic implements IDSComponentInstance {
private static final long serialVersionUID = 1L;
private DBContainerGraphic container;
//only used as identifer. no methods on component are ever called from this reference!!
private Long component;
/*
* Note, a sourced connection connects its source here.(source to source)
*
* 1-N uni-directional Set with foreign key. It means the component
* instance table will not have any entry but a foreign key will be used in
* the connections table.
* uni-directional means the component instance class has a reference to
* the connections class, but the connections class does not have a
* reference to the component instance class.
*
*/
private Set<DBFunctionConnection> sourceConnections;
private Set<DBFunctionConnection> targetConnections;
public DBComponentInstance(Point location, Dimension size) {
super(location,size);
this.container = new DBContainerGraphic();
this.sourceConnections = new HashSet<DBFunctionConnection>();
this.targetConnections = new HashSet<DBFunctionConnection>();
}
/*
* We do not want proxy for graphical objects. So we dont need package
* scope here.
*/
private DBComponentInstance(){
}
}
and this
Code:
public final class DBFunctionConnection implements IDSFunctionConnection, Serializable, Cloneable{
private static final long serialVersionUID = 1L;
/*
* Hibernate spec version 3.2.0.ga section 4.1.2 states that transitive
* reattachment of detached objects requires an identifier. So we have one.
*/
private Long id; // identifier
private List<DBBendpointGraphics> bendPoints = new ArrayList<DBBendpointGraphics>();
private int sourceLocationX,sourceLocationY;
private int targetLocationX,targetLocationY;
//private DBCircuit circuit;
private Long circuit;
public DBFunctionConnection(Point sourceLocation, Point targetLocation) {
super();
assert sourceLocation != null;
assert targetLocation != null;
this.sourceLocationX = sourceLocation.x;
this.sourceLocationY = sourceLocation.y;
this.targetLocationX = targetLocation.x;
this.targetLocationY = targetLocation.y;
}
/*
* We do not want proxy for graphical objects. So we dont need package
* scope here.
*/
private DBFunctionConnection(){
}
}
The metadata is as follows
Code:
<hibernate-mapping>
<class name="com.rgdsft.hib.core.DBFunctionConnection" table="dbfunctionconnection" lazy="false">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="sourceLocationX" not-null="true" access="field"/>
<property name="sourceLocationY" not-null="true" access="field"/>
<property name="targetLocationX" not-null="true" access="field"/>
<property name="targetLocationY" not-null="true" access="field"/>
<property name="circuit" column="circuitid" access="field" type="long"/>
<list name="bendPoints" access="field" lazy="false" cascade="all-delete-orphan">
<key column="FunctionConnectionID" not-null="true"/>
<list-index column="sortOrder"/>
<one-to-many class="com.rgdsft.hib.core.DBBendpointGraphics" />
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<joined-subclass name="com.rgdsft.hib.core.DBComponentInstance" extends="com.rgdsft.hib.core.DBContainableGraphic" table="dbcomponentinstance" lazy="false">
<key column="OBJECT_ID"/>
<property name="component" column="componentid" access="field" type="long"/>
<many-to-one name="container" column="ContainerID" not-null="true" unique="true" access="field" cascade="all" lazy="false"/>
<set name="sourceConnections" access="field" lazy="false" cascade="all,delete-orphan">
<!-- key column is not null since connectons must always have both ends when saved -->
<key column="S_ComponentInstanceID" not-null="true"/>
<one-to-many class="com.rgdsft.hib.core.DBFunctionConnection"/>
</set>
<!-- cascade is all,delete-orphan. Connection life is totally dependent on
the component instance. Plus, conns are removed from comp instance prior to
deletion, so delete-orphan is required else hibernate will attempt to remove
comp instances from conn which will cause constraint exception due to the
not-null constraing on the comp instance id -->
<set name="targetConnections" access="field" lazy="false" cascade="all,delete-orphan">
<!-- key column is not null since connectons must always have both ends when saved -->
<key column="T_ComponentInstanceID" not-null="true"/>
<one-to-many class="com.rgdsft.hib.core.DBFunctionConnection"/>
</set>
</joined-subclass>
</hibernate-mapping>
I am getting this exception
Quote:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.rgdsft.hib.core.DBFunctionConnection._targetConnectionsBackref
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:284)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.engine.Cascade.cascade(Cascade.java:97)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:332)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:304)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:217)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.engine.Cascade.cascade(Cascade.java:97)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:332)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:304)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:217)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:564)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:552)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:544)