Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SanderBenschop opened this issue May 12, 2017 · 0 comments
Open

Comments

@SanderBenschop
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant