We have the simplest CRUD task with JPA 1.0 and JAX-WS.
Let's say we have an entity Person.
@Entity
public class Person
{
@Id
private String email;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(insertable = false, updatable = false)
private ReadOnly readOnly;
@Column
private String name;
@XmlElement
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
@XmlElement
public Long getReadOnlyValue()
{
return readOnly.getValue();
}
// more get and set methods
}
Here is scenario. Client make Web Service request to create person. On the server side everything is straightforward. And it does work as expected.
@Stateless
@WebService
public class PersonService
{
@PersistenceContext(name = "unit-name")
private EntityManager entityManager;
public Person create(Person person)
{
entityManager.persist(person);
return person;
}
}
Now client tries to update person and this is where, as for me, JPA shows its inconsistence.
public Person update(Person person)
{
Person existingPerson = entityManager.find(Person.class, person.getEmail());
// some logic with existingPerson
// ...
// At this point existingPerson.readOnly is not null and it can't be null
// due to the database.
// The field is not updatable.
// Person object has readOnly field equal to null as it was not passed
// via SOAP request.
// And now we do merge.
entityManager.merge(person);
// At this point existingPerson.getReadOnlyValue()
// will throw NullPointerException.
// And it throws during marshalling.
// It is because now existingPerson.readOnly == person.readOnly and thus null.
// But it won't affect database anyhow because of (updatable = false)
return existingPerson;
}
To avoid this problem I need to expose set for readOnly object and do something like this before merge.
Person existingPerson = entityManager.find(Person.class, person.getEmail());
person.setReadOnlyObject(existingPerson.getReadOnlyObject()); // Arghhh!
My questions:
- Is it a feature or just inconsistence?
- How do you (or would you) handle such situations? Please don't advice me to use DTOs.