ORM Bug or me?

below I have a many-to-many relationship. however when I add records
to the linking table via ORM it lets me add duplicated keys when I
should get an error, but I don't!

my user entity...

component output="false" persistent="true" {

       property name="user_id" column="user_id" type="numeric"
ormtype="int" fieldtype="id" generator="increment";
     property name="user_name" column="user_name" type="string"
length="120" ormtype="string";
     /* link table */
     property name="monsters" fieldtype="many-to-many" CFC="monsters"
FKColumn="user_id" singularname="monsters"
     inversejoincolumn="monster_id" linktable="monstersCollected";

        users function init() output=false{
                return this;
        }

}

Hi Gyln,

When creating many-to-many with link-table and no duplicate record
then you have to compose link-table with composite primary key

HERE is simple CFC based solution. Orm will generate a link-table
with composite primary key.
component entityname="UserMonster" persistent="true" accessors="true"
table="monstersCollected"
{
    property name="Users"
       fieldtype="id,many-to-one"
       cfc="User"
       cascade="all"
       fkcolumn="user_id";

    property name="monsters"
       fieldtype="id,many-to-one"
       cfc="monster"
       cascade="all"
       fkcolumn="monster_id";
}

After this then each entity should know about each other ... like this

user = new or get
monster = new or get

user.AddMonsters(monster)
monster.AddUsers(user)

Thats it, hibernate know what to with this .... Hope this will help
you

Thanks
Sana

Hi Sana,

It does, but ORM creates this link table for me from
linktable="monstersCollected" are you saying that I would also create
the entity cfc for link table or that I remove the property from both
these cfcs i.e. users and monsters and create a new cfc linking these
up in your example UserMonster and let that create the relationships
and linktabke.

why do I see examples only using

           inversejoincolumn="monster_id"
linktable="monstersCollected";

whats the difference?

thanks

Glyn

so just played about with this and yes it working perfectly how I want
it. :0 Thanks!

Hi,

Good to hear that its working for you now :slight_smile: