I have a Foo
entity that's linked to a Status
entity in a one-by-one relationship:
@Entity
@NamedQueries({
@NamedQuery(name = "Foo.findById", query = "select o from Foo o where o.fooId = :fooId")
})
public class Foo implements Serializable {
@Id
@Column(name="FOO_ID")
private Long fooId;
@OneToOne(mappedBy = "foo")
private Status status;
public Long getFooId() {
return fooId;
}
public void setFooId(Long fooId) {
this.fooId = fooId;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
@Entity
public class FooCurrentStatus implements Serializable {
@Id
private Long fooId;
@OneToOne
@JoinColumn(name = "FOO_ID")
private Foo foo;
public Long getFooId() {
return fooId;
}
public void setFooId(Long fooId) {
this.fooId = fooId;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
if (foo != null) {
this.fooId = foo.getFooId();
}
}
}
I understand this is not completely wrong because when I fetch a Foo
instance I automatically get its status.
From the application point of view, the status is read-only. It is managed elsewhere, by some other unrelated process that modifies the database information.
Unfortunately, the side effect is that whenever the status changes my managed entity gets outdated.
How can I instruct EJB/JPA to grab a fresh status?