I found similar questions, but they did not answer my question.
I have two entities with a many-to-one relationship - unidirectional. But most importantly, the relationship is lazy. Because it is correct to use a lazy connection, everyone knows it.
Code:
@Entity
public class User implements BaseEntity {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private City city;
}
@Entity
public class City implements BaseEntity {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
}
interface BaseEntity {
void setId(Long id);
Long getId();
}
I wrote a method that allows you to search by the transferred fields of the entity. An example of how this works:
public class Search<T extends BaseEntity> {
public List<T> getByFields(T entity, List<String> fieldNames) {
// create criteria with passed field name by reflection
}
}
The problem is that problems start when you need to search and related objects - by creating related queries.
The following entry is used to send the associated field: "city.id" and the problem is that when I transfer the essence of the related object (City) it is in a proxy and I cannot get id by reflection from City.
My function works perfectly if you specify:
@ManyToOne(fetch = FetchType.EAGER)
private City city;
But it will greatly affect performance, since I have a lot of related objects. Therefore, I want to solve this problem for a lazy load.
I know that this is not an easy task. But perhaps there is some opportunity to somehow get around this problem.
Aucun commentaire:
Enregistrer un commentaire