Menu

Transient Collections

2020-11-07
2020-11-17
  • Martin Smith

    Martin Smith - 2020-11-07

    Hi,

    Can someone give me an example of how to work with transient collections? What I want is a Collection<Item> that's filled by an action. There is no need for this result to be persisted, it's just info for the user. In this case Item is a persistent class but I could use a plain bean with a couple of properties.

    I've tried various things (@Transient on property and getter, declaring the collection field as transient etc.) and the collection always appears empty. I can see the action setting the collection property but nothing shows up when calling refreshCollections() on the view.

    I noticed you can't set this property with setValue() on the view but it is shown in the UI so I have probably done something wrong.

    Thanks,

    Martin

     
  • Javier Paniza

    Javier Paniza - 2020-11-10

    Hi Martin:

    Define the collection without @OneToMany. Fill the collection by code and then inject the container object (the entity or transient object) into the view with getView().setModel(myContainerObject). If it does not work, put here the code of your entity and your action.


    Help others in this forum as I help you.

     
  • Martin Smith

    Martin Smith - 2020-11-11

    Hi,

    Thanks but I still see no items in the collection, must be doing something wrong. The code below is a minimal version of what I have.

    Martin

    In the entity class Template, which is where I want to show the results:

      @Transient
      private Collection<Thing> chosen = new ArrayList<Thing>();
    

    Plus a getter and setter. The Thing class is just a bean with a couple of properties.

    In the action (just an example for testing purposes):

        Thing thing1 = new Thing("Thing 1");
        Thing thing2 = new Thing("Thing 2");
        List<Thing> things = new ArrayList<Thing>();
        things.add(thing1);
        things.add(thing2);
    
        Template t = (Template) getView().getEntity();
        t.setChosen(things);
        getView().setModel(t);
    
     

    Last edit: Martin Smith 2020-11-11
  • Javier Paniza

    Javier Paniza - 2020-11-12

    Hi Martin:

    Add @ElementCollection annotation and it will work with your code above. Thus;

     @Transient
     @ElementCollection
      private Collection<Thing> chosen = new ArrayList<Thing>();
    

    OpenXava for entity collection (those without @ElementCollection) does not use the getter of the class but it get the data directly from database using JPA. In this way we can paginate, filtering and ordering. So if you use @ElementCollection OpenXava will get the data from the getter.


    Help others in this forum as I help you.

     
  • Martin Smith

    Martin Smith - 2020-11-12

    Thanks a lot, that works exactly as I wanted.

    Just out of interest should @ListProperties work with this approach? It doesn't really matter as I'll just define the bean to have the ones I want but I got the exception below when I added it.

    org.openxava.util.XavaException: collection-view for chosen is already defined in view chosen of 
        org.openxava.view.meta.MetaView.addMetaViewCollection(Unknown Source)
        org.openxava.component.parse.AnnotatedClassParser.processAnnotations(Unknown Source)
        org.openxava.component.parse.AnnotatedClassParser.addCollection(Unknown Source)
        org.openxava.component.parse.AnnotatedClassParser.addMember(Unknown Source)
        org.openxava.component.parse.AnnotatedClassParser.parseMembers(Unknown Source)
        org.openxava.component.parse.AnnotatedClassParser.parse(Unknown Source)
        org.openxava.component.MetaComponent.parse(Unknown Source)
        org.openxava.component.MetaComponent.get(Unknown Source)
        org.openxava.application.meta.MetaModule.getLabelFromModel(Unknown Source)
        org.openxava.application.meta.MetaModule.getLabel(Unknown Source)
        org.openxava.util.meta.MetaElement.getLabel(Unknown Source)
        org.apache.jsp.naviox.selectModules_jsp._jspService(selectModules_jsp.java:194)
    

    Martin

     
  • Martin Smith

    Martin Smith - 2020-11-15

    Is there any chance this stopped working in 6.4.2? I just updated and can no longer see anything after populating the list.

    Thanks,

    Martin

     
  • Javier Paniza

    Javier Paniza - 2020-11-16

    Hi Martin:

    ust out of interest should @ListProperties work with this approach?

    @ListProperties works with element collections.

    org.openxava.util.XavaException: collection-view for chosen is already defined in view chosen of

    Attach here the complete code of the class, I'll examine it to see where the problem is.

    Is there any chance this stopped working in 6.4.2?

    I just try the example I use to solve your problem and it works perfectly with 6.4.2. Maybe the updated process is incomplete. Do you follow all the steps? Including the updateOX ant task execution?


    Help others in this forum as I help you.

     
  • Martin Smith

    Martin Smith - 2020-11-17

    Hi,

    I got the exception when I switched from a transient Bean class to making it a Collection<Item> where Item is an entity and has views defined. I switched back to a bean so this is no longer a problem.

    I've solved my other problem, for some reason the action was extending ViewDetailAction. I changed it back to ViewBaseAction and it started working. I don't think it's related to 6.4.2.

    Thanks,

    Martin

     

    Last edit: Martin Smith 2020-11-17

Log in to post a comment.