0

I'm using hibernate 3.6.10 and I'm facing this situation.

I have:

table A: a_id, a_name, a_desc
table B: b_id, b_name, a_id

and one A can be in many Bs

I want to do a one way ManyToOne join

So I've got:

@Entity
@Table(name = "B")
public class B {
@ManyToOne(fetch = FetchType.EAGER)
@Cascade(CascadeType.ALL)
@JoinColumn(name = "a_id")
private A a;

@Column(name = "b_name")
private String b_name;

...
}


@Entity
@Table(name = "A")
public class A {

@Id
@Column(name = "a_id")
private Integer id;
....
}

Then I do:

sessionFactory.getCurrentSession().createCriteria(B.class).list();

PROBLEM is: It does the outter left join but it only brings 1 of entity A for all of Bs.

It should bring:

row 1: B: 1 | 1stName | 32 (a_id & a_name & a_desc)
row 1: B: 2 | 2ndName | 69
row 1: B: 3 | 3rdName | 45
row 1: B: 4 | 4thName | 94
row 1: B: 5 | 5thName | 32

but it brings:

row 1: B: 1 | 1stName | 32 (a_id & a_name & a_desc)
row 1: B: 2 | 2ndName | 32
row 1: B: 3 | 3rdName | 32
row 1: B: 4 | 4thName | 32
row 1: B: 5 | 5thName | 32

It looks like Hibernate does the correct query with the correct join, which if I run it manually in the DB brings as expected. It sounds to me I'm having a problem on how hibernate/annotations are configured.

I can see in the logs the correct values for Entity B, but for some reason it only uses one (id 32) for all the Bs.

Any ideas?

I hope this question makes seance.

thanks!

pepepapa82
  • 167
  • 2
  • 10
  • You can enable the sql logs in the hibernate and run the same query against the db. My guess, all the Bs have been assigned to the same A. – Zeus Dec 05 '13 at 19:42
  • I can see in the logs the correct Bs. It looks like if hibernate is absorbing them between the result from the DB and the conversion to object. – pepepapa82 Dec 06 '13 at 20:31

0 Answers0