Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Problem with sharing objects in different lists

    Hi,

    i have a problem with my model. I have 2 database tables: projects and persons. They have a many to many relationship. My model looks like this:

    In class Project i save a list of Persons in this Project:
    Java Code:
    private List<Person> persons;
    other properties like name, description, etc ...
    In class ProjectsModel i save the list of all Projects:
    Java Code:
    private List<Project> projects = new ArrayList<Project>();
    private void readProjects() {
    		Query query = em.createQuery("select p from Project p");
    		projects = query.getResultList();
    }
    In class PersonsModel i save the list of all Persons:
    Java Code:
    private List<Project> persons = new ArrayList<Person>();
    private void readPersons() {
    		Query query = em.createQuery("select p from Person p");
    		this.persons = query.getResultList();
    }
    And a Person class of course with properties of a person.

    Cause of the queries i now have 2 different models. Changing a property of a Person from the PersonsModel instance doesn't change the persons property in a personslist in a project cause they are different objects. And i don't know how to change it so that persons in a project are only pointing to the appropriate persons in personsModel and thus sharing the same objects.

    Any suggestions would help greatly :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Have another class, say ModelCreator, that creates the classes you mentioned and pass a single List<Person> to both your Project and PersonsModel classes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Is this a desktop app?
    Why are you holding onto this data? Is there a reason you're cacheing it?

  4. #4
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    Thanks for the answers!

    @ JosAH and Tolls

    Doesn't both classes then share the exact same list?
    So that when i add a person to PersonsModel it is added to all projects?
    What i like is a project having some subset of PersonsModel (references to it) so that if i delete a person from PersonsModel it is deleted from all projects that person is in. but if i delete a person from a project, that person is only deleted from that project.

    I need this for an RCP app with a gui. In this gui i have a treeView with all the projects and the persons as leafs of this tree. The projects can be edited, deleted and made new. And i can add and delete persons from projects. I also have a second view that can be opened where the user can edit, delete and make new persons.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Two options.
    Either a single model that both parts reference, or each part has their own view on the data which is refreshed on a change.
    Both will require a Controller to control change events.

    If you want deletions to percolate through then the controller will need to be listening in on the parts of the model for change events (inserts and deletes). This will then inform any interested view that the model has changed so it should consider redrawing. If it's option 1 (shared model) then it can listen in on the model. If it's option 2 (separate models) then it can listen in on the gui, which will fire off a DataChange event whenever it saves stuff to the database.

    If you update the database at the same time as the changes are made in the GUI then option 2 might make sense, but that depends on your architecture (where the data is, and how much needs refreshing). Option 2 also has the advantage of having the database handle the problem of cascade deletions (ie delete a person, and have that person deleted from all projects). Option 1 has the least amount of db interaction, so might make sense if network traffic would be a problem.

  6. #6
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    I currently do it so that the 2nd view has a PropertyChangeEvent which the MainView is listen to. The MainView then gets the changed person and i then search in the tree for the unique id of that person and if found change it. But this seems ugly, i would really like to have one model but i don't know how i can merge it. The problem being that a project does only have a subset of the list of persons.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AmFreak View Post
    I currently do it so that the 2nd view has a PropertyChangeEvent which the MainView is listen to. The MainView then gets the changed person and i then search in the tree for the unique id of that person and if found change it. But this seems ugly, i would really like to have one model but i don't know how i can merge it. The problem being that a project does only have a subset of the list of persons.
    Doesn't my suggest (reply #2) work for you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    Made a new project and tried this:

    Java Code:
    public class ModelCreator {
    
    	private List<Person> persons = new ArrayList<Person>() ;
    
    	public Project makeProject()
    	{
    		Project project = new Project(persons);
    		return project;
    	}
    	
    	public PersonsModel makePersonsmodel()
    	{
    		PersonsModel pm = new PersonsModel(persons);
    		return pm;
    	}
    Project Class
    Java Code:
    	public Project(List<Persons> persons) {
    		this.persons = persons;
    	}
    PersonsModel Class
    Java Code:
    public PersonsModel(List<Person> persons)  {
    		this.persons = persons;
    }
    Java Code:
    public class ListTest {
    	public static void main(String[] args) {
    		ModelCreator modelCreator = new ModelCreator();
    		PersonsModel personsModel = modelCreator.makePersonsModel();
    		Project project = modelCreator.makeProject();
    But as i said they now share the exact same list and so if i add some person to a project it's added to every project and to the personsModel - if i delete one person from a project it's deleted everywhere. I must have mistunderstood something?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AmFreak View Post
    But as i said they now share the exact same list and so if i add some person to a project it's added to every project and to the personsModel - if i delete one person from a project it's deleted everywhere. I must have mistunderstood something?
    Maybe I didn't pay enough attention, but isn't this what you wanted?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Your Controller/Creator/whatever is doing the change listening needs to identify that a Person has been deleted and then delete that Person from all Projects.
    All Projects should have their own list of people involved in the project.
    Then your creator/Controller/whatever informs the other view that the data has changed.
    The other view then rebuilds itself...

  11. #11
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    @ JosAH

    No, the situation is this: I have a person and a project table with a many to many relationship. So that persons can be in projects (in 0...*) and a project can have (0..*) persons in it. I now have a mainView when the app ist started. In this view i show all projects in a treeView with the persons in projects as the leafs of that project. I can make and delete a project and change it's properties in this view. I can also add to or delete persons from a project. All these changes are done to the model of the view and the database. Trough dataBinding the view updates itself when i change the model. I also have a command which opens a second view where i get a list with all persons (meaning all persons that are in the persons table). In this view i can add, delete or change persons. When i add, change or delete a person from that view i again only change the model and the database. The view then updates itself through dataBinding. The problem now is that there are also persons in the mainView (persons that are in projects). But the mainView isn't updated cause it's not the same model then the one in the persons view. I do this now manually with a listener in the mainview which listens to changes in the persons view (Posting 6). That's why i would like to have one model so that the mainView would update itself if a person that is at least in one project is changed.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    So.
    Share the model.
    And have a Controller control chnages to it.
    The views are simply windows on the model.

  13. #13
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So.
    Share the model.
    That's my original question at the beginning of the thread, cause i don't know how to merge the 2 models so i have one that i can share then.
    Last edited by AmFreak; 07-06-2011 at 03:36 AM.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Well, it'll still be two things because there's no entirely sensible way to do it (if you don't want to be querying the db all the time that is).
    One List of projects and one List of users.
    The users under each Project will be references to a User object in the User List...which takes care of the edit side of things.
    When a User is deleted then the thing that controls this model will handle the removal of that User from any associated projects...indeed, if the Users in the User List also hold a List of Projects they are part of this would be dead easy.

  15. #15
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    The users under each Project will be references to a User object in the User List...which takes care of the edit side of things.
    But how can i make it so that the persons in a project are references to a person in the personsList? I currently create different persons for each list by reading the data from the database (see #1).

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Well, don't.
    Populate the full User list, then populate the users in a Project from that list.
    Of course this might involve not using JPA.

    Should that pose a problem then (and this is the way JPA should be used) query the database when your front end needs updating.
    Do not hold onto the data!

    So the Projects view makes a request for Projects as it needs them, and on a refresh call from the controller.
    Similarly with the users view.

    Which is what I think I suggested higher up.

  17. #17
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    So the Projects view makes a request for Projects as it needs them, and on a refresh call from the controller.
    I don't know if i understand you correctly, say i delete a stock from the stocksview, what would the controller then do?
    Last edited by AmFreak; 07-07-2011 at 01:53 PM.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    The controller would (which would be what handled the stock deletion in the first place) would inform any interested views the their data needed updating.
    The view would then request from the controller a refreshed list.

    If performance holds up then this is the neatest way. Should performance prove to be a problem on the refresh you can then look at partial refreshes...but that would be slightly more complex. Do the basic "give me a fresh set of data" version first.

  19. #19
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default

    So everytime someone deletes a person from the personsview the controller would inform the mainView. The mainView then requests a new list from the controller. The controller queries the database ("select p from Project p") and the view sets this as the new input of the treeViewer?

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    That's the simplest route.
    Write it neatly and you should be able to easily change how the controller manages data should that prove to be too heavy on network traffic.

Page 1 of 2 12 LastLast

Similar Threads

  1. Comparing objects: lists and maps
    By LucasH in forum Advanced Java
    Replies: 7
    Last Post: 05-24-2011, 06:41 PM
  2. Objects problem ...
    By santa in forum New To Java
    Replies: 4
    Last Post: 05-14-2011, 04:14 PM
  3. a problem with objects
    By p0o0o0ya in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 08:32 AM
  4. Problem with Objects
    By javadam in forum New To Java
    Replies: 4
    Last Post: 01-27-2011, 11:39 PM
  5. Problem sending linked lists
    By deepthought015 in forum Networking
    Replies: 1
    Last Post: 04-26-2009, 12:47 PM

Posting Permissions

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