0

I have a problem populating a detail view from the database.

application.xml snippet:

<module name="ModuleB">
        <model name="B"/>
        <view name="ViewB"/>
        ...
        <mode-controller name="DetailOnly"/>
</module>

I have three entity classes:

@Entity
@Table(name="A")
class A {
    @OneToMany(mappedBy = "a")
    @ListProperties("col1")
    @CollectionView("cs")
    private Collection<C> cs;//+getter/setters
}

@Entity
@Table(name="A")
@View(name = "ViewB", ...)
class B {
    @OneToMany(mappedBy = "a")
    @ListProperties(...)
    @CollectionView("ViewC")
    private Collection<C> cs;//+getter/setters
}

@Entity
@Table(name="C")
@View(name = "ViewC", ...)
class C {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "a_id")
    private A a;
}

I want to read an existing B instance from the database by clicking on a link, then editing/modify it. When I set the model object of the view with getView().setModel() or even using getView().findObject(), on the screen everything looks good, the collection shows its proper content. On the other hand when i try to save it back, in the save action the getView().getEntity().getCs() collection is null. What do I have to do to make the view being correspond to the entity behind?

I am using OpenXava 5.0.1, java 1.7 Important note: I am not allowed to change OpenXava version, since it is a legacy project.

My editing (20170126) I've made a new class to avoid a reference problem:

@Entity
@Table(name="C")
@View(name = "ViewC", ...)
class D {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "a_id")
    private B a;
}

and modified the class B accordingly:

@Entity
@Table(name="A")
@View(name = "ViewB", ...)
class B {
    @OneToMany(mappedBy = "a")
    @ListProperties(...)
    @CollectionView("ViewC")
    private Collection<D> cs;//+getter/setters
}

But the result is the same: The records of the subview (Collection ViewC) are loaded from the DB, and shown on the screen correctly, but I get an error message if I want to edit the Collection ViewC ( eg: add a new entry ): "Impossible to execute Save action: Impossible to create: an object with that key already exists" + As before: in the save action the getView().getEntity().getCs() collection is null

SzGyD
  • 88
  • 7

1 Answers1

0

OpenXava uses JPQL to obtain collection data instead of just calling the collection getter, this is for allowing filtering and ordering. The problem here is that the query sentence generated by OpenXava gets the data but the getter of the JPA entity not. Surely JPA/Hibernate is not very happy with a mappedyBy to a reference to another type. That is you write:

class B {
  @OneToMany(mappedBy = "a")
  @ListProperties(...)
  @CollectionView("ViewC")
  private Collection<C> cs;//+getter/setters
}

Where a is a reference to A, not to B. Given that A and B map the same table, why not to use just one entity?

javierpaniza
  • 677
  • 4
  • 10
  • I added some supplement to my original question. Do You have any idea to the solution in this case? Thank You in advance! – SzGyD Jan 26 '17 at 09:34
  • I still think that your enties does not work well with JPA/Hibernate. Test your entities without OpenXava, just with a class with a main(). Search the main entity with JPA and get the collection. Does it work? – javierpaniza Jan 31 '17 at 13:18