I have the following relationship:
@Entity
public class SomeEntity {
//...
@EmbeddedId
private SomeEntityIdentity id;
@OneToOne
@NotFound(action = NotFoundAction.EXCEPTION) //This is the important bit
@JoinColumns({
//...
})
private OtherEntity example;
//...
}
Then, I use Spring data's findOne()
to grab my entity by the Id:
SomeEntityIdentity id = new SomeEntityIdentity();
id.setAttribute1(1);
id.setAttribute2(new BigDecimal(123));
return this.someEntityRepository.findOne(id);
The problem is that no exception is thrown if OtherEntity
is not found, as findOne()
simply returns null. Even if I set @OneToOne(optional = false)
I still get a null from findOne()
, when I was excepting only OtherEntity
to be null.
I believe an exception should be thrown. Does anyone any have ideas?
Thank you!
Edit: Identity and Repository classes below.
@Embeddable
public class SomeEntityIdentity implements Serializable {
private int attribute1;
private BigDecimal attribute2;
public void setAttribute1(int attribute1) {
this.attribute1 = attribute1;
}
public void setAttribute2(BigDecimal attribute2) {
this.attribute2 = attribute2;
}
}
public interface SomeEntityRepository extends JpaRepository<SomeEntity, SomeEntityIdentity> {
}