[yes, I'm still working on this.]
1) Mapping a many-to-many with Set & composite-element as specified in sec 6.2. See hbm below.
2) In order to implement equals & .hashcode in the the composite-element class (AdAUserRoleXref),
don't I have to use the parent element tag within the composite-element? Otherwise, adAUseridMain is always null. Without the equals & .hashcode methods, hibernate deletes the entire collection and re-adds everyone again, plus the new item.
3) I get an extra composite-element in the collection on the load AdAUseridMain that is
not in the db. The extra item does not have a AdAUseridMain (its null) so this causes a npe in AdAUseridMain.hashcode. This extra item does have a role.
4) Christian Bauer's reply on the old forum was to
Quote:
Make sure that your component properties are all "not null" if you're using a Set for the the collection.
Does this mean,
<property not-null="true" column="CREATE_DATE" name="createDate" type="java.sql.Timestamp" />
Did that but didn't help. Christian must mean something else?
Thanks,
Jeff Boring
Code:
console messages:
INFO: Hibernate 2.1 beta 1
...
Hibernate: select adauseri0_.SKEY as SKEY0_, adauseri0_.MANAGER_ID as MANAGER_ID0_, adauseri0_.USERID as USERID0_, adauseri0_.PWD as PWD0_, adauseri0_.LNAME as LNAME0_, adauseri0_.M_INIT as M_INIT0_, adauseri0_.FNAME as FNAME0_, adauseri0_.NOTES_ADDRESS as NOTES_AD8_0_, adauseri0_.TITLE as TITLE0_, adauseri0_.PHONE as PHONE0_, adauseri0_.MC as MC0_, adauseri0_.BUDGET as BUDGET0_, adauseri0_.LOCATION as LOCATION0_, adauseri0_.DEPARTMENT as DEPARTMENT0_, adauseri0_.DIVISION as DIVISION0_, adauseri0_.SITE as SITE0_, adauseri0_.BUSINESS_UNIT as BUSINES17_0_, adauseri0_.BUYER_CODE as BUYER_CODE0_, adauseri0_.ACT_STAT_CD as ACT_STA19_0_, adauseri0_.LST_LOGIN_DATE as LST_LOG20_0_, adauseri0_.LST_UPD_DATE as LST_UPD21_0_, adauseri0_.LST_UPD_USERID as LST_UPD22_0_, adauseri0_.BU_GRP_CD as BU_GRP_CD0_, adauseri0_.OUT_OF_OFFICE_IND as OUT_OF_24_0_, adauseri0_.OUT_OF_OFFICE_TEXT as OUT_OF_25_0_, adauseri0_.EMAIL_ADDRESS as EMAIL_A26_0_, adauseri0_.PAGER_PHONE as PAGER_P27_0_, adauseri0_.PAGER_PIN as PAGER_PIN0_, adauseri0_.HOME_PHONE as HOME_PHONE0_, adauseri0_.FAX_PHONE as FAX_PHONE0_, adauseri0_.CELL_PHONE as CELL_PHONE0_, adauseri0_.LOCATION_CD as LOCATIO32_0_, adauseri0_.BAY_LOC as BAY_LOC0_, adauseri0_.CUBE_NUM as CUBE_NUM0_, adauseri0_.EMPLOYEE_TYPE as EMPLOYE35_0_, adauseri0_.ORG_CD as ORG_CD0_, adauseri0_.ORG_LVL as ORG_LVL0_, adauseri0_.USER_TYPE_CD as USER_TY38_0_, adauseri0_.INTRA_EXTRA_CD as INTRA_E39_0_, adauseri0_.PROFILE_SKEY as PROFILE40_0_, adauseri0_.ADDRESS_SKEY as ADDRESS41_0_ from tgaddba3.AD_A_USERID_MAIN adauseri0_ where adauseri0_.SKEY=?
Hibernate: select ad_a_use0_.CREATE_DATE as CREATE_D2___, ad_a_use0_.CREATE_USERID as CREATE_U3___, ad_a_use0_.LST_UPD_DATE as LST_UPD_4___, ad_a_use0_.LST_UPD_USERID as LST_UPD_5___, ad_a_use0_.ROLE_SKEY as ROLE_SKEY__, ad_a_use0_.USER_SKEY as USER_SKEY__, adrrole1_.ROLE_SKEY as ROLE_SKEY0_, adrrole1_.APPLICATION_ID as APPLICAT2_0_, adrrole1_.ROLE_CATEGORY as ROLE_CAT3_0_, adrrole1_.ROLE_DESC as ROLE_DESC0_, adrrole1_.ROLE_SORT_SEQ as ROLE_SOR5_0_, adrrole1_.ACTIVE_IND as ACTIVE_IND0_, adrrole1_.CREATE_DATE as CREATE_D7_0_, adrrole1_.CREATE_USERID as CREATE_U8_0_, adrrole1_.LST_UPD_DATE as LST_UPD_9_0_, adrrole1_.LST_UPD_USERID as LST_UPD10_0_ from tgaddba3.AD_A_USER_ROLE_XREF ad_a_use0_, tgaddba3.AD_R_ROLE adrrole1_ where ad_a_use0_.USER_SKEY=? and ad_a_use0_.ROLE_SKEY=adrrole1_.ROLE_SKEY(+)
java.lang.NullPointerException
at com.siemens.pg.pma.vo.bp.AdAUserRoleXref.hashCode(AdAUserRoleXref.java:90)
<test-code>
AdAUserRoleXref xref = null;
AdRRole role = null; Set s = null; Iterator it = null;
Session sess = DatastoreHBN.singleton().getSession();
Transaction xac = sess.beginTransaction();
GregorianCalendar today = new GregorianCalendar();
try {
AdAUseridMain u = (AdAUseridMain) sess.load( com.siemens.pg.pma.vo.bp.AdAUseridMain.class, new Integer(1125) );
role = (AdRRole) sess.load( com.siemens.pg.pma.vo.bp.AdRRole.class, new Integer(201) );
xref = new AdAUserRoleXref();
xref.setCreateUserid( u.getUserid() );
xref.setLstUpdUserid( u.getUserid() );
xref.setCreateDate( today.getTime() );
xref.setLstUpdDate( today.getTime() );
xref.setAdAUseridMain( u );
xref.setAdRRole( role );
s = u.getAdRRoles();
s.add( xref );
sess.flush();
}
catch (Exception e ){
e.printStackTrace();
}
finally {
xac.commit();
sess.close();
}
</test-code>
<hibernate-mapping>
<class name="com.siemens.pg.pma.vo.bp.AdAUseridMain" table="AD_A_USERID_MAIN" >
<id name="skey" type="java.lang.Integer" column="SKEY" unsaved-value="any" >
<generator class="assigned" />
</id>
<property name="managerId" type="java.lang.Integer" column="MANAGER_ID" length="9" />
<property name="userid" type="java.lang.String" column="USERID" not-null="true" length="8" />
...
<!-- bi-directional many-to-many association to AdRRole -->
<set name="adRRoles" lazy="false" table="AD_A_USER_ROLE_XREF" cascade="all" >
<key column="USER_SKEY" />
<composite-element class="com.siemens.pg.pma.vo.bp.AdAUserRoleXref" >
<parent name="adAUseridMain" />
<property not-null="true" column="CREATE_DATE" name="createDate" type="java.sql.Timestamp" />
<property not-null="true" column="CREATE_USERID" name="createUserid" type="java.lang.String" />
<property not-null="true" column="LST_UPD_DATE" name="lstUpdDate" type="java.sql.Timestamp" />
<property not-null="true" column="LST_UPD_USERID" name="lstUpdUserid" type="java.lang.String" />
<many-to-one name="adRRole" class="com.siemens.pg.pma.vo.bp.AdRRole">
<column name="ROLE_SKEY" />
</many-to-one>
</composite-element>
</set>
</class>
</hibernate-mapping>