1

Yet another JPAContainer question, this time it's about committing OneToMany relationships.

@Entity(name="CLAZZES")
public class Clazz implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="CLAZZ_ID", nullable=false)
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @Column(name="NAME")
    private String name;

    @ManyToOne(optional=false)
    @JoinColumn(name="YEAR_ID", referencedColumnName="YEAR_ID")
    private Year year;

    @OneToMany(mappedBy="clazz", targetEntity=CourseWork.class, fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval=true)
    private Collection<CourseWork> courseWorks;

    @OneToMany
    private List<PupilCoursework> pupilCourseworks;

    public Clazz() {}   

    // Getters & Setters
}

@Entity(name="COURSEWORKS")
public class CourseWork implements Serializable  {

    @Column(name="COURSEWORK_ID")
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @Column(name="NAME")
    private String name;

    @Column(name="CLAZZ")
    private long clazzID;

    @ManyToOne(optional=false, fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name="CLAZZ_ID", referencedColumnName="CLAZZ_ID", insertable=true, updatable=true)
    private Clazz clazz;

    public CourseWork() {}
    // Getters & Setters
}

I'm using JPAContainer for a table showing all classes. And in a window, you can add existing courseworks to this class.

When setting Clazz#courseWorks, changes aren't committed to the database.

The container seems to accept the value, because in the Vaadin table, the values are displayed correctly!

Although I'm obviously having a bidirectional relationship between clazz and coursework, the container doesn't seem to commit the change of this only field to the underlying DB (Apache Derby). The other fields in the container are committed as expected.

I've tried everything to appropriately set the corresponding item property in the container. E.g.

// protected OptionGroup selectedClassWorks;

// upon submit of the form:
Collection<CourseWork> courseWorkItems = engine.getItems(selectedClassWorks.getValue());
clazz.getItemProperty("courseWorks").setValue(courseWorkItems);

I don't really think it matters, but if it does: I'm using a converter for the option group which converts forth and back between a collection of itemid's and a collection of CourseWork objects.

As an alternative, I've also tried to get the EntityItem's entity and set the collection there.

The result stays the same: container okay, database not okay.

Atmocreations
  • 9,923
  • 15
  • 67
  • 102

0 Answers0