Hi,
I have a 'Credential' table in my DB which is indexed by a composite Id of:
MemberId
CredentialTypeId
I have a 'Member' table, which has a foreign key reference to the Credential table, with it's MemberId column.
So, a member can have multiple Credentials. For example, for a Member with memberID=1000, we could have the following entries in the Credential table:
Member ID | Credential Type ID
--------------------------------------
1000 | 1
1000 | 2
1000 | 3
I am having trouble with my hibernate mappings when I create a new Member. I want the MemberID to be autogenerated for Member and for this MemberID to be set as the MemberID on the nested collection of Credentials. The Credential Type Id (the second id of the composite index) is set explicitly on each Credential object before saving. I can not set memberID explicitly as it is generated at save in the hibernate layer.
Here are my mappings:
MEMBER:
<class name="com.entity.Member" table="dbo.Member" lazy="false">
<id name="memberId">
<generator class="identity"/>
</id>
<bag name="credentials" table="dbo.MemberCredential">
<key column="memberID" />
<one-to-many class="com.entity.Credential"/>
</bag>
</class>
CREDENTIAL:
<class name="com.entity.Credential" table="dbo.MemberCredential" lazy="false">
<composite-id>
<key-property name="credentialTypeId" column="credentialTypeId"/>
<key-property name="memberId" column="memberId"/>
</composite-id>
<property name="username" column="Username"/>
<property name="password" column="Password"/>
</class>
I have played around with various settings. The mappings above seem the "closest" to correct but when I call saveOrUpdate(Member), the MemberID column of the nested array of Credential is null - not the auto generated MemberID that I am hoping for.
Am I referencing Credential correctly in Member? Do I need to explicitly tell it to use memberID? Have I mapped my Credential table correctly?
Any help would be much appreciated.
I am using hibernate 3.1, and JDK 1.4.2
Thanks!
James
|