Results 1 to 2 of 2
  1. #1
    garnachito is offline Member
    Join Date
    May 2012
    Posts
    19
    Rep Power
    0

    Default Create an array of objects containing objects array ?

    Hi everyone I'm really stuck with this, so I have a table call teachers which have many courses, and courses that have many teachers, and I have the relationship table in between called teacher_courses

    teachers -> teachers_courses <- courses

    so basically what I need is to get an array of teachers in which I need to have an array of courses that they have some thing like:

    Java Code:
    ["teachers":
                   [  {'name': 'John',
                       'last_name': 'Doe',
                       'courses':[{'name': 'math'},{'name': 'programming'}]},
                      {'name': 'Jane',
                       'last_name': 'Doe',
                       'courses':[{'name': 'math'},{'name': 'programming'}]}
    ]
    and so on....

    this is my code right now: which only returns obviously the teachers table:

    Java Code:
    public List<Teacher> getTeachers(){
    		
    		return jdbc.query("select * from teachers", new RowMapper<Teacher>(){
    			public Teacher mapRow(ResultSet rs, int rowNum) throws SQLException {
    				Teacher teacher = new Teacher();
    				teacher.setId(rs.getInt("id"));
    				teacher.setName(rs.getString("name"));
    				teacher.setLast_name(rs.getString("last_name"));
    				return teacher;
    			}
    		});
    	}
    Thanks in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Create an array of objects containing objects array ?

    So, for each Teacher you need to then request the associated set of courses.
    You could do that as a single query, covering all Teachers, order by the teacher id, and populate the teacher objects in a loop, maybe.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 02-10-2015, 06:08 PM
  2. Replies: 2
    Last Post: 02-10-2015, 06:06 PM
  3. Replies: 3
    Last Post: 07-31-2014, 09:47 AM
  4. create a static array of objects in java?
    By aidanmack in forum New To Java
    Replies: 10
    Last Post: 01-02-2014, 04:28 PM
  5. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 02:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •