2

I'm struggling with unnecessary SQL select query when trying to get entity reference with Spring data repository using Hibernate as persistence provider. My case concerns many times discussed question about using entityManager.getReference() in order to get proxy object with just initialized id value here and here. But this doesn't work as described.

I have following environment and code

  1. Spring Boot JPA project with Hibernate
  2. One dead simple entity Foo
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "d20_foo")
public class Foo {

    @Id
    @Column(name = "id")
    private Long id;

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

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

In my service code i'm just invoking getReference just like this

@Service
@Transactional
public class MyService {

@PersistenceContext
private EntityManager entityManager;

...
public void getFooLink() {
...
    Foo foo = entityManager.getReference(Foo.class,1L);
...
}
...

}

and in hibernate logs in debug mode I see, that, once I hit getReference, following select query is performed

Hibernate: 
    select
        foo0_.id as id1_0_0_,
        foo0_.name as name2_0_0_ 
    from
        d20_foo foo0_ 
    where
        foo0_.id=?

Why is that? Why query is performed instead of just returning proxy object?

I didn't hit any additional fields (like invoking getName() on returned object) and did all as was described in reference implementations.

I don't need extra query - Foo entity is a read-only object and I need it just as a referenced object to another entity.

2 Answers 2

4

At last I guess the answer was simple.

It was a debug mode (maybe especially in IntelliJ IDEA) that fooled me and showed these kind of logs. When I retried to monitor this code without debugging (just writing logs to console) - everything is fine. Maybe when I'm doing debug my IDE call some property accessors (maybe of a name field) that causes Hibernate to perform select queries to initialise proxy.

So at last everything works as expected

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried to declare JPA annotation on getter methods instead?

@Entity
@Table(name = "d20_foo")
public class Foo {

    
    private Long id;
    private String name;

    @Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.