Hi Everyone,
I have many-to-one unidirectional relationship between two objects as follows:
Code:
@Entity
@Table(name = "A")
public class A implements Serializable {
private Long id;
private B bObject;
@Id
public Long getId() {
return id;
}
public void setId(Long _id) {
id = _id;
}
@ManyToOne
@JoinColumn(name = "a_id", insertable = false, updatable = false, nullable = true)
public B getBObject() {
return bObject;
}
public void setBObject(B _bObject) {
bObject = _bObject;
}
}
@Entity
@Table(name = "B")
public class B implements Serializable {
private Long id;
private Long aId;
@Id
public Long getId() {
return id;
}
public void setId(Long _id) {
id = _id;
}
@Column(name="a_id")
public Long getAId() {
return id;
}
public void setAId(Long _aId) {
aId = _aId;
}
}
in the session bean I am getting List<A> as follows:
Code:
...
Query query = em.createQuery("select a from A a left outer join a.bObject as b ");
query.setFirstResult(start);
query.setMaxResults(MSG_PER_PAGE);
List<A> result =query.getResultList();
...
The problem is that every time when there is no record in table B that corresponds to record in table A I am getting the javax.persistence.EntityNotFoundException: Unable to find B with id 123.
I also notice that if record in table B exists then I am getting just one join query in the log
Code:
select A0_.id as id10_0_, B1.id as id_11_1_ from A A0_ left outer join B B1_ on A0_.id=B_.a_id
otherwise I am getting join query + additional select from table B.
Code:
select A0_.id as id10_0_, B1.id as id_11_1_ from A A0_ left outer join B B1_ on A0_.id=B_.a_id
select B0_.id as id11_0_, B0_.a_id as a_id2_11_0_ from B B0_
where B0_.a_id=?
I am running Jboss 4.0.4.
Any help is appreciated.
Thanks,
Ed[/code]