Irfan Mulic
Irfan Mulic

Reputation: 1206

JPA updating model with subset of fields

I am using play framework 1.2.4 and I have a profile page with many elements that are separated in segments (basic info, education, work experience etc).

Person model looks like this:

@Entity
@Table(name="Persons")
public class Person extends GenericModel implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Id",nullable=false,unique=true)
    private int id;

    @Column(name="FirstName",nullable=false)
    private String firstName;

    @Column(name="MiddleInitial")
    private String middleInitial;

    @Column(name="LastName",nullable=false)
    private String lastName;

    @Column(name="Born",nullable=false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private java.util.Date born;

    @Column(name="Sex",nullable=false)
    private String sex;

    @Email
    @Column(name="Email",nullable=false,unique=true)
    private String email;

    @Column(name="City")
    private String city;

...

In controller I have this:

public static void updatePerson(Person person) {
              person.validateAndSave(); 
 }

I would like to send subset of updates to the model i.e. one part of the form that have a submit option have only person.firstName, person.lastName, person.dob

in the view I am sending parameters using a dot notation for JPA object binding i.e.(person.firstName, person.lastName, etc.) From play documentation :

http://www.playframework.org/documentation/1.2/controllers#objectbinding

Problem I am having is that it looks like this type of binding requires all fields of the model so I am getting NULL violations on those that are not being set in particular view.

My current code is using JPA.em().createQuery solve it but I am getting different problems with types...

I am looking for better way ? Any thoughts?

Upvotes: 1

Views: 491

Answers (1)

Codemwnci
Codemwnci

Reputation: 54914

You have a few options here

1) Create Form objects, that are only kept for a transaction. Each part of your wizard process will have a different object, and the final part of the wizard process will convert all those objects into your final Model object.

2) Second option is to allow null values in your Model and accept that your database may contain incomplete object data

3) Don't save to the database until the final step, but save the data that you collected on previous forms into hidden fields on the current form so that on your final step, all data is passed in as an object and can simply be saved.

Upvotes: 1

Related Questions