0

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<FieldHolder> data) {
        // create criteria with passed field name and value by reflection
    } 
}

class FieldHolder {
    private String fieldName;
    private Object value;
    /**
     * "true" - means that the field "value" contains id related object
     * "false" - elementary type like: String, Wrapper, Primitive
     */
    private boolean isRelationId;
}

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.

FreeOnGoo
  • 868
  • 1
  • 8
  • 26
  • what is the error when you try to access the field by reflection ? – davidxxx Mar 05 '19 at 19:13
  • @davidxxx since I am performing data acquisition on reflection, it falls due to the exception that it cannot find the field, in particular, the field "id" of the class City. Well, this is understandable, since during debugging, we don’t have the City object itself - but its proxy. – FreeOnGoo Mar 05 '19 at 19:15
  • That is understandable, I was just interested into the specific exception thrown. Otherwise, reflection is maybe not the best way, can you give an example of usage of `getByFields()` ? – davidxxx Mar 05 '19 at 19:22
  • @davidxxx otherwise, this problem cannot be solved, since only field names need to be transmitted. And the function code is very long, but it is trivial, just search for the name of the field and its value. – FreeOnGoo Mar 05 '19 at 19:24
  • I ask the question because I have difficulty to understand the usage of this method : `public List getByFields(T entity, List fieldNames)` It takes an entity of type `T` and it returns a list of `T`. It differs from your early explanations. No mandatory to post the whole code but having meaningful snippet matters. – davidxxx Mar 05 '19 at 19:26
  • @davidxxx sorry, I first added the wrong code – FreeOnGoo Mar 05 '19 at 19:40
  • 1
    https://stackoverflow.com/questions/2593722/hibernate-one-to-one-getid-without-fetching-entire-object – amFroz Mar 05 '19 at 19:42
  • Are you performing these operations inside the same transaction? – Pallav Jha Mar 06 '19 at 06:50
  • @ObiWan-PallavJha yes – FreeOnGoo Mar 06 '19 at 06:59

0 Answers0