views:

1857

answers:

3

Hi, banging my head here and thought that some one out there might be able to help. Have Tables below.

Bucket(
 bucketId smallint (PK)
 name varchar(50)
)

BucketUser(
 UserId varchar(10) (PK) 
 bucketId smallint (PK)
)

The composite key is not the problem thats ok I know how to get around this but I want my bucket class to contanin a IList of BucketUser. I read the online reference and thought that I had cracked it but havent. The two mappings are below

-- bucket --

 <id name="BucketId" column="BucketId" type="Int16" unsaved-value="0">
  <generator class="native"/>
 </id>
 <property column="BucketName" type="String" name="BucketName"/>

<bag name="Users" table="BucketUser" inverse="true" generic="true" lazy="true">
  <key>
    <column name="BucketId" sql-type="smallint"/>
    <column name="UserId" sql-type="varchar"/>
  </key>
  <one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>
</bag>

-- bucketUser --

A: 

What error are you getting? If you really cut-and-pasted that mapping then the solution may be as simple as replacing the comma in

<one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>

with a period.

Jamie Ide
Hi thanks for the reply. The main on is ""Foreign key (FK32947277369BF2:Bucket [BucketId, UserId])) must have same number of columns as the referenced primary key (Bucket [BucketId])"} So basically the bucketid is the primary key on the bucket table and its part of the foreigh key on the bucketUser table
+3  A: 

The key is the foreign key to the containing entity, not the primary key.

You have two options:

  • the class represents an independent entity, having an own id. It could be referenced from other classes, is always in the same table and could be loaded independently.
  • or it is a part of another entity with no independent identity. If referenced by other classes it is always in a separate table. It could not (easily) loaded independently from its parent entity.

Bucketuser is an idependent entity. It has its own mapping definition and you reference it using one-to-many. You get a composite key in your case, but I would avoid this.

<!-- reference to BucketUser. There is not table attribute needed. -->
<bag name="Users" inverse="true" generic="true" lazy="true">
  <key>
    <!-- foreign key -->
    <column name="BucketId" sql-type="smallint" />
  </key>
  <one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>
</bag>

<!-- BucketUser mapped as an independent entity -->
<class name="BucketUser" ... >
  <!-- here is the composite id, try to avoid this -->
  <composite-id>
    <key-property name="BucketId">
    <key-property name="UserId">
  </composite-id>
</bag>

Bucketuser is a dependent part of Bucket. The foreign key to the Bucket is the primary key at the same time:

<!-- The table is defined on the fly by the table attribute -->
<bag name="Users" table="BucketUser" inverse="true" generic="true" lazy="true">
  <key>
    <column name="BucketId" sql-type="smallint" />
  </key>
  <!-- use composite-element to define the contents of the table -->
  <composite-element>
    <!-- define the contents of the BucketUser here -->
    <property name="UserId" sql-type="varchar"/>
  </composite-element>
</bag>

It depends on your case which strategy is appropriate.

Stefan Steinegger
A: 

Hello to you all! I'm having some problems while using hibernate to map my database tables. An error keeps coming out that says: The content of element type "one-to-many" must match "EMPTY".

Can anyone please tell me what the problem is and how to solve it?? I'd really appreciate it!

jorge