Skip to content

Support for private field modification or 'nested merged forms' #73

@SanderBenschop

Description

@SanderBenschop

The situation is as follows:

We have an entity called Publication that contains a collection of PublicationTitle entities (which are implementations of Text entities):

public class Publication extends BaseEntity {
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "entityId", nullable = false, updatable = false)
    private Set<PublicationTitle> titles = new HashSet<>();

   ...
}
@Entity
@DiscriminatorValue("publication-title")
public class PublicationTitle extends Text {
}
public abstract class Text extends BaseEntity {
    @ManyToOne
    private Language language;

    @Column(columnDefinition = TEXT)
    private String value;

    @Column(insertable = false, updatable = false)
    private Long entityId;

    @Column(insertable = false, updatable = false)
    private String type;

   ...
}

In our request when updating the Publication, we sent all the (possibly modified) instances of PublicationTitle that belong to the given publication:

{
	"id": 2494,
	"publicationType": "NEWS",
	"titles": [{
		"id": 6469,
		"language": 1,
		"value": "Test!"
	}]
}

That maps to the following form object:

public class PublicationBasicDataForm {

    public PublicationType publicationType;

    @ContainsSupportedLanguages
    @Valid
    @BeanCollection(elementType = PublicationTitle.class, beanCollectionUsage = BeanCollectionUsage.CLEAR)
    public Set<TextForm> titles;
}

Because we have OrphanRemoval on and we update the existing hash set, we can replace the contents in the database without getting duplicates. This does mean however that we have to create a setter for the id field in BaseEntity, because BeanMapper needs to set that in order for Hibernate to know that we intend to edit an existing entity. We would prefer not to do this, as this allows anyone to set the id of an entity, which we want Hibernate to handle itself.

Rob and I discussed this and we see two possible solutions:

  • Allow a marker annotation on a field in the form object to indicate that we want to allow a private field to be accessed and set via reflection.

  • Created a 'nested merged form', Beanmapper would fetch the appropriate PublicationTitles from the database and apply the changes to the appropriate PublicationTitle based on the id in the form object.

It would be nice to investigate what solution would fit the Beanmapper architecture and philosophy best.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions