I am working with legacy code. I have entity A
and entity B
class, but because entity B
is being referenced many places, I try not to make changes on entity B
. Followings are classes for these two entities.
Entity B
has a foreign key column which is primary key of the entity A
and in the database table, the foreign key(aId) is associated with many primary keies of the table b(id)
.
What I need is a collection of A(id range between 1 - 10000 for example) that includes collection of B corresponding to aid when I make a query against table_a. Current implementation is get a collection of A(has large number of rows) first and then loop through each A in the collection then call dao.findB(aId) to set collection of B for the A. It causes many trips to the database. I try to minimize this trip. For example, if A has 50,000 rows, dao.findB(aId) will be called 50,000 times.
table_b
----------
id table_a_id
1 2
2 2
3 2
4 3
5 3
@Entity
@Table(name = "table_a")
public class A{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
}
@Entity
@Table(name = "table_b")
public class B{
private static final long serialVersionUID = 1L;
@Id
@Column(name="id", unique = true, nullable = false)
@GeneratedValue
private Long id;
@Column(name="table_a_id")
private Long aId;
}