I'm quite fond of the features Roo M2 provides. While looking at the samples I did not find a sample of a n:m mapping - How would that work in Roo ?
Regards.
Paul
I'm quite fond of the features Roo M2 provides. While looking at the samples I did not find a sample of a n:m mapping - How would that work in Roo ?
Regards.
Paul
Hi Paul,
Roo currently offers two commands to generate JPA relationships:
which generates a @ManyToOne JPA relationship mapping, andCode:add field reference
which generates a @OneToMany JPA relationship.Code:add field set
Take a look at the clinic.roo script to see how to use them.
So to get a n:m relationship you would typically create three tables:
Order
-OneToMany->OrderOrderLine
OrderOrderLine
-orders @ManyToOne->Order
-orderlines @ManyToOne->OrderLine
OrderLine
-OneToMany->OrderOrderLine
Hope this helps.
-Stefan
Great thread thanks alot... I've been stumbling on when to use 'add field set' and 'add field reference' ever since I began using Roo. Now if I can only figure out the most straightforward way of making a 1:1 relationship by using Roo I'll be all set.
Hi Paul,
Basically you are already using the right command for this. Just use the 'add field reference' command to essentially create a one-to-one mapping as seen in the clinic.roo script.
On the MVC side this will create a dop-down list where you can choose only one element as opposed to the 'add field set' which creates a multi-selection box on the MVC side.
Hope this helps.
-Stefan
Hi Stefan,
would you mind pasting the roo commands for this n:m mapping, I am struggeling to get this done here. Basically I want to have a community which has a number of persons in it and a person who can be member of different communities. But I am not sure how to write this in roo.
Thanks,
Philipp
@pbussche,
Below the script for a true n:m mapping:
I hope this helpsCode:project --topLevelPackage com.foo.opensource persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY entity --name ~.domain.Community --testAutomatically field string name entity --name ~.domain.Person --testAutomatically field string name field set --fieldName communities --element ~.domain.Community --cardinality MANY_TO_MANY field set --element ~.domain.Person --fieldName members --class ~.domain.Community --cardinality MANY_TO_MANY controller all --package ~.web.
Cheers,
Stefan
Cool, thanks. That works.
But it seems now that I have to store on the community side which person is part of it and on the person side which community he is a member of. Well I overwrote one setter method to automatically do that when a person is created and a community is specified but is that the recommended way of doing it ?
I guess this is more persistence basics than roo, sorry![]()
@Stefan
...
I'm trying ROO. Tutorial studying and so on.
Your example is exactly what I need now, but... it doesn't work for me!
I mean:
- i create three person: A, B and C;
- i create two communities: X and Y;
- i associate A and B to X and C to Y;
- from the communities list, I can't see the association done.
It looks like data isn't consistent. Can you help me please??
Can you share your script so we can see what exactly you are seeing in your application?
Stefan Schmidt
Software Engineer, Spring Roo
SpringSource - a division of VMware
twitter @schmidtstefan
Thanks for your reply Stefan!
Ok with BusinessFunction now.Code:// Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:15:57 project --topLevelPackage it.celi persistence setup --provider HIBERNATE --database HYPERSONIC_PERSISTENT database properties list database properties set --key database.url --value jdbc:hsqldb:${user.home}/dd4-cloe-roo database properties list entity --class ~.domain.BusinessFunction field string code --notNull --sizeMin 1 --sizeMax 30 field string specialRequests --sizeMax 100 test integration controller scaffold ~.web.BusinessFunctionController selenium test --controller ~.web.BusinessFunctionController perform test // script --file /home/fabio/wedding.roo quit // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:17:36 // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:24:42 entity --class ~.domain.UserRole field string --fieldName id --notNull --sizeMin 1 --sizeMax 30 field string --fieldName description --sizeMax 100 field set --fieldName allowedFunctions --element ~.domain.BusinessFunction quit // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:34:43 // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:36:51 controller scaffold --entity ~.domain.UserRole --class ~.web.UserRoleController selenium test --controller ~.web.UserRoleController quit // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:39:37 // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:45:51 perform eclipse quit // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:59:29
Then... I create a UserRole. I update a field, like description. I list all roles and....... roo duplicates it. I find the 'old' with his relations with BusinessFunctions and the new one.
I can't understand.