Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DataSource usage in "real" ??

    I am actually fully stuck with the DataSource meaning and dont know how to match it into "Real" world!
    Even if i now try to share some database tables - with my "main" server (because i have an existing jsf app running on a seam server) how should i get the oneToMany or manyToMany relationship to work as they are within another table ?

    So my problem is that (i try to simplify as much as possible):

    A User can have several roles or be in serveral groups !
    Made my server entities with hibernate - which means the following:

    Code:
    User.class
    
    private int id;
    private String name;
    ......
    private Set<Groups> belongingGroups,
    private Set<Roles> userRoles;
    
    @ManyToMany (mappedBy="users", targetEntity=xxxxx.Group.class)
    public getBelongingGroups()
    
    @ManyToMany(mappedBy="users", targetEntity=xxxxx.Role.class)
    public getUserRoles()
    
    
    Role.class
    .....
    @ManyToMany(targetEntity=com.amergy.cmt.client.client.model.server.User.class)	
    @JoinTable(name="User_Role", joinColumns={ @JoinColumn(name="RoleID") }, inverseJoinColumns={ @JoinColumn(name="UserID") })	
    private java.util.Set<User> users = new HashMap.......
    this then produces several tables ...
    Code:
    User (with all Fields from User)
    Role (with all Fields from Role)
    User_Role(with the UserId and the RoleID)
    Now i made two datasource out of that - one UserDS and one RoleDS !
    How can i now make any component with SmartClient and those Datasources to assign the UserDS to one (or several) Roles ???
    Do i need to make a 3rd DataSource ? and if yes - how does the Update of the data then happen by the SmartClient server ?
    Do i need to make the mapping "manually" (in a custom datasource) ??

    Do i need to rewrite the whole structure - to fit into a datasource (which would mean to write some converters - from / to Database) ?

    Can someone point me to the right direction - how to use those DataSource(s) within such a "Real" world application (relationships) !!

    Many Thanks

    #2
    here is what works for me:

    In your User datasource class, make the user id the primary key

    Code:
    DataSourceIntegerField userIdField = new DataSourceIntegerField("id","User Id");
    userIdField.setPrimaryKey(true);
    userIdField.setHidden(true);
    The role ds will need to have a foreign key which references the the userId in the user ds.

    Code:
    DataSourceIntegerField primaryKeyField  = new DataSourceIntegerField("roleId","Role ID");
    primaryKeyField.setPrimaryKey(true);
    primaryKeyField.setHidden(true);
    DataSourceIntegerField foreignKeyField = new DataSourceIntegerField("id","User Id");
    foreignKeyField.setForeignKey(MyUserDS.getInstance().getID() + ".id");
    foreignKeyField.setHidden(true);
    So now the role ds knows about the user ds primary key field...

    here is a way you could use it, say in a clickhandler on a list grid.

    Code:
    myuserGrid.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent e) {
    	ListGridRecord r = myuserGrid.getSelectedRecord();
            myRoleListGrid.fetchRelatedData(r,MyUserDS.getInstance());
        }
    });
    This will make a fetch call to the dataurl of the datasource attached to your role list grid, which should be the role ds... The method fetchRelatedData also passes in the User ds, which is how the role ds can get the primary key of the selected record in the user list. Does that make sense? If not do a search in the forum... I believe there's quite a few examples better than this one. There's alot of info around here, just have to be persistent in your searches ;) Good luck!

    Comment


      #3
      ManyToMany not OneToMany !!

      Thanks mickmcl but the problem i am facing with - from understanding point of view as within your description its generally fine but if you use hibernate the database structure is given !!

      What you described is a oneToMany topic - then you can have all the id's within the other table - for reference - but to link a ManyToMany item you need to have a joining table - and therefore i cannot set a foreign key from one datasource to the other without using the "joining table" - but i have not found and info regarding that in the forum - thats why i was asking how someone else made a ManyToMany relationship based on hibernate tables !

      Comment


        #4
        Mick really did answer your question correctly. Try another read (and thanks Mick).

        Comment


          #5
          I read it again - and i think the answer is correct for a OneToMany relationship as you do not need a joining table !

          But my main concern is regarding ManyToMany - as there you cannot set a foreign key (not directly)!

          If i search the forum for a ManyToMany - i only get my post and something regarding the gwtrpc datasource !!!

          Comment


            #6
            I now tried to build everything using the Hibernate connector - and now i think i have a logical issue - as i cannot see any Fetch Operation which takes place ??

            I have two hibernate entities defined - the Team and the UserItem !
            The Team has a Set<UserItem> !

            Within the Team i have in the Hibernate config the following:
            Code:
                   <set name="users" cascade="save-update,lock">
                    	<key column="teamID"/>
                    	<one-to-many class="com.amergy.cmt.client.server.customds.UserItem"/>
                    </set>
            in the DataSource xml file i defined it like that - and the foreignKey points to my useritem Datasource definition (Both have a primary key) !

            In the TeamDS.xml i have the following
            Code:
                    <field name="users" title= "User Items" multiple="true"
                           type="usermanagement_user" 
                           javaClass="com.amergy.cmt.client.server.customds.UserItem"
                           foreignKey="usermanagement_user.pk"
                    />
            If the user now selects the Team within another Listgrid - i want all the users to show up in a second ListGrid !

            Now i use it like Mick posted (Thanks again for you help)
            Code:
                    ListGridRecord r = myuserGrid.getSelectedRecord();
                    myUserItemListGrid.fetchRelatedData(r,UserManagementDS.getInstance());
            But the second ListGrid (with the UserItems) keeps being empty (if i use the TeamDS) or always filled up with all the values if i use the UserManagementDS on the second ListGrid ???
            It seems that he dont know about the mapping ??? - how can i achive that with the Hibernate connector ??
            Last edited by jhollerer; 24 Jan 2010, 18:37.

            Comment


              #7
              If a grid is intended to show users, it's DataSource should be the user DataSource. The DataSource passed to fetchRelatedData() should be the other, related DataSource, not the user DataSource.

              If that's not working, you have a typo in a DataSource ID or foreignKey declaration, or a problem of that nature. Also, look for warnings in your Developer Console, and closely inspect the requests and responses in the RPC tab.

              Comment


                #8
                Originally posted by mickmcl

                Code:
                myuserGrid.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent e) {
                	ListGridRecord r = myuserGrid.getSelectedRecord();
                        myRoleListGrid.fetchRelatedData(r,MyUserDS.getInstance());
                    }
                });
                Related withe that part of code. If I have a DynamicForm with a multiple SeletItem field, I could do something like:


                Code:
                myuserGrid.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent e) {
                        MyUserDS ds = MyUserDS.getInstance()
                	ListGridRecord r = myuserGrid.getSelectedRecord();
                        myRoleListGrid.fetchRelatedData(r,ds);
                        
                        myFormField.setOptionalDataSource(ds);
                
                    }
                });

                This will fill the form's field with all the users and checks the users that belong to the selected role in the role listgrid, right?

                Thanks

                Comment

                Working...
                X