Reputation: 1541
I am facing a design decision in my GWT + Hibernate project.
I will explain my question using a Company entity.
@Entity
@Table(name = "Company")
public class Company extends LightEntity implements BaseSerializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private int id;
@Column(name = "Code")
private String code;
@Column(name = "Name")
private String name;
@Column(name = "Address")
private String address;
@Column(name = "ContactNumber1")
private String contactNumber1;
@Column(name = "ContactNumber2")
private String contactNumber2;
@Column(name = "EMail")
private String email;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "company", cascade = CascadeType.ALL)
private List<CompanyRegistration> companyRegistrations = new ArrayList<CompanyRegistration>();
public Company() {
}
public Company(String code, String name) {
this.setCode(code);
this.setName(name);
}
// getters & setters
}
A valid Company object must always have a valid code and name. Rest of the properties are optional. Hence, I have provided a two-args constructor to ensure the creation of a valid object.
Now.
I am used to validate data in setters. I also use these setters in my constructor(s). These ensure that a new object always has a valid state. This is/was my plan in implementing the Hibernate entities.
However, GWT-RPC mandates default noargs constructor so that it can be used for deferred binding. But that will also allow anyone to create an empty object, which is not desirable in most of my Hibernate entities. For example, a Company must always be constructed with a Code and a Name. A noargs constructor defies that rule.
Now, Hibernate has implemented a standard (JSR-303, if my memory doesn't betray me) for validation framework. It lets you invoke validations on an already created object.
My problem has arisen due to the noargs constructor. If it had not been mandatory, I would have done away with validations in setter.
If I implement validations in setters, that means that an invalid object will fail as fast as possible. I won't need to invoke a validation on an object on client side. But, an empty object (created using the noargs constructor) can't be validated that way. It might make it way to the service, implying that I also have to implement validations, a lot of which might be the same as the setters, in the service layer.
If I implement the validation framework, then I have to invoke validation on each object I create on the client side. If I forget to validate one, it can go to service layer in an invalid state. However, I can easily use the same framework on the service layer.
One solution that I have thought of is to implement validations in setters anyway, but also use a Validator in the service layer, before sending an object to persist in database. That way, the client side will be restricted by the setters automatically. On the other hand, if an empty object is sent anyhow to the server side, it will be caught by the Validator on service layer. An exception will be thrown to the user in both the cases.
I think this is not the best solution, however. So, I would like to get one.
EDIT - I am anyways using annotations to put constraints on entity fields, so that Hibernate can use them for validation before persisting an object.
Upvotes: 0
Views: 399
Reputation: 64541
If your problem only stems from the mandatory zero-arg constructor for GWT-RPC, note you can make this constructor inaccessible to developers (even private
if you want); GWT-RPC will access it using reflection whichever its visibility.
Upvotes: 1