facebook

Struts issues

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #235514 Reply

    Chris Parsons
    Participant

    I am trying to get to grips with the MyEclipse/Struts integration, and am struggling with something?

    If I use a simple model, I have a table – say ‘table1’ – with a foreign key – say ‘fk’ – that has a contraint on another table – say ‘table2’ – that shows a description for that key.

    When I edit table1, I want to show a list box on the JSP that lists all the descriptions in table2, allowing the user to select one to update the fk field in table1 (a pretty standard approach for a web form?)

    My JSP page has a form bean that I use to store the data with getters and setters for the data (I am using Hibernate)

    It appears that the getFk method generated by MyEclipse gets the object (record) from table2? I can write a method to get all the records from table2 and store them in the form bean, and then use a <logic:iterate> tag to display them, so I know they are there but cannot work out how to use them with a <html:select> tag to list them and return the correct value when something is selected?

    Can someone help me with a simple example? The method I use for getting the descriptions from table2 returns an array of table2 objects, is this the correct approach? I noticed that where there is a one-to-many relationship from table1 to other tables that the methods generated by MyEclipse return ‘Sets’ ie getServiceSet() but I cannot get my head around how to use these – the model I am thinking about is JSP with ‘header’ fields and multiple records listed underneath

    Parent name
    Parent address
    
    Child1.name Child1.age
    Child2.name Child2.age
    ...

    Thanks

    Chris Parsons

    #235524 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev.

    so I know they are there but cannot work out how to use them with a <html:select> tag to list them and return the correct value when something is selected?

    I’d suggest making the value of the html:option’s the actualy ID of the description, and the text for it, whatever the description is. Then name your html:select something like descriptionID, and give a int (or String depending on what your PK type is for that table) property on your form bean that takes the value when the form is submitted.

    Can someone help me with a simple example? The method I use for getting the descriptions from table2 returns an array of table2 objects, is this the correct approach? I noticed that where there is a one-to-many relationship from table1 to other tables that the methods generated by MyEclipse return ‘Sets’ ie getServiceSet() but I cannot get my head around how to use these – the model I am thinking about is JSP with ‘header’ fields and multiple records listed underneath

    I didn’t follow this, but the purpose of Sets is to garuntee the uniqueness of the list.

    #235593 Reply

    Chris Parsons
    Participant

    I am trying to post a clearer explanation of my problem but your BB keep bombing (on preview as well)

    http://www.myeclipseide.com/PNphpBB2+file-posting.html

    Forbidden
    You don’t have permission to access /index.php on this server

    I am including SQL statements and code in ‘code’ tags and I have also tried ‘quote’ tags to no avail

    Luckily I had saved the text, but don’t appear to be able to send it as an attachment either, any ideas??

    I could upload it to our website and send you a url I suppose but this is a bit of a pain!

    Saved as a text file it is only 5Kb…so I can’t imagine it’s a size problem?

    Thanks

    Chris Parsons

    #235601 Reply

    Riyad Kalla
    Member

    Chris,
    It’s the SQL that’s causing the posting problem, try and comment it out or break it so it isn’t valid SQL but I should still be able tot ell what it says.

    #235690 Reply

    Chris Parsons
    Participant

    Blimey, your forum software is hard work!

    After an hour hacking my post around I have given up, and created a web page for it, try having a look
    at http://www.torbay.gov.uk/myeclipse

    Thanks

    Chris Parsons

    #235691 Reply

    Riyad Kalla
    Member

    ahh Chris thanks for doing that, I understand the confusion:
    Hibernate works by mapping your tables to Objects, subsequent o2o, m2o or o2m relationships between tables are not references by the actual field IDs, they are references to the objects themselves and then Hibernate in the background magically takes care of everthing for you.

    So for example, if you have a table:
    user

    with a field “user_status_fk”

    and another table “user_status”

    that had a list of User status-es-es-es, then your User object would generate get/setUserStatus(UserStatus) methods, not get/setUserStatus(int) methods. The whole idea of hibernate is to use Objects and have them magically taken care of for you, so our mapper honors that.

    If you desperately want to use ints, you will need to add the get/set(int) methods yourself in the Abstract base class.

    #235759 Reply

    Chris Parsons
    Participant

    Right – this is what I had worked out looking at the code

    What is confusing me is that if I edit a record where one of the fields is an object, how do I set the property of the key field?

    My code has a bean that I load the subunit object into, then call a JSP – I can edit the properties of the subunit, but how do I use/update
    the property that refers to the businessunit object?

    I am using an <html:select> tag to list businessunit descriptions (and set values as the businessunit ‘id’) and am returning the selected
    ‘id’ value in an integer field as you suggested in an earlier post but then what do I do with it when I want to update the subunit?

    Create a new businessunit object, set the id to the returned value and save that in the subunit? Is this the idea?

    Sorry, this is turning into more of a post about Hibernate than MyEclipse but this is the last piece of the puzzle!

    Thanks

    Chris

    #235775 Reply

    Riyad Kalla
    Member

    How I’ve historically done this in the past is:
    BusinessUnit bu = // get the business unit using Hibernate somehow with the ID from the submited form
    subunit.setBusinessUnit(bu);
    // now commit the transaction and close the session.

    #236040 Reply

    Chris Parsons
    Participant

    Thank you, I follow this, but my issue is getting the fk data to the ‘saveSubunit’ method – I have created a field called ‘businessUnitForeignKey’ that is private to the formbean ‘subunitEditForm’ – I have also passed an array of BusinessUnit(s) to the ‘subunitEditForm’ so I can show a listbox to select from (see below)

    
    <html:select name="subunitEditForm" property="businessUnitForeignKey" >
      <html:optionsCollection property="businessunits" value="id" label="description"/>
    </html:select>

    My issue is how do I get the selected value from ‘businessUnitForeignKey’ when I call the saveSubunit method?

    Pass it as a (hidden) parameter from the JSP page? Add it to the ‘Subunit’ definition?

    My business logic has a getBusinessUnitByPrimaryKey method but I need the ‘businessUnitForeignKey’ to use as the primary key and every time I test it in the saveUnit method I get a null

    Thank you for your help

    Chris

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Struts issues

You must be logged in to post in the forum log in