简体   繁体   中英

JPA/Hibernate double select on OneToOne Find

While learning JPA/Hibernate I stumbbled upon something unexpected. I am getting an unnecessary double select query to the db (see buttom).I have a simple OneToOne setup.

This is my Contact entity:

@Entity
@Table(name = "contact")
public class Contact {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "contact_id", nullable = false, insertable = false, updatable = false)
  private Long id;

  @Column(name = "name", nullable = false)
  private String name;

  @OneToOne
  @JoinColumn(name="fk_address_id", referencedColumnName="address_id")
  private Address address;

  public Contact(String name, Address address) {
    this.name = name;
    this.address = address;
  }

  // getters/setters

}

My Address entity:

@Entity
@Table(name = "address")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "address_id", nullable = false, insertable = false, updatable = false)
  private Long id;

  @Column(name = "location", nullable = false)
  private String location;

  @OneToOne(mappedBy = "address")
  private Contact contact;

  public Address(String location) {
    this.location = location;
  }

  // getters/setters

}

This is how I ran my code:

  private EntityManager em;

  @Before
  public void setup() {
    em = EntityManagerFactoryCreator.getEntityManagerFactory().createEntityManager();
  }

  @Test
  public void createContactWithAddress() {
    Address address = new Address("Made");
    Contact contact = new Contact("Jan", address);

    em.getTransaction().begin();
    em.persist(address);
    em.persist(contact);
    em.getTransaction().commit();

    em.close();
    em = EntityManagerFactoryCreator.getEntityManagerFactory().createEntityManager();

    Contact managedContact = em.find(Contact.class, 1L);
  }

}

It is probably something stupid, but what is causing the double select?

Hibernate: 

    drop table address if exists
Hibernate: 

    drop table book if exists
Hibernate: 

    drop table contact if exists
Hibernate: 

    create table address (
       address_id bigint generated by default as identity,
        location varchar(255) not null,
        primary key (address_id)
    )
Hibernate: 

    create table book (
       book_id bigint generated by default as identity,
        category varchar(255),
        release_date date,
        summary varchar(255),
        title varchar(255) not null,
        primary key (book_id)
    )

Hibernate: 

    create table contact (
       contact_id bigint generated by default as identity,
        name varchar(255) not null,
        address_address_id bigint,
        primary key (contact_id)
    )
Hibernate: 

    alter table book 
       add constraint UK_g0286ag1dlt4473st1ugemd0m unique (title)
Hibernate: 

    alter table contact 
       add constraint FKrc0ixa9b11b9tv3hyq0iwvdpt 
       foreign key (address_address_id) 
       references address
Hibernate: 
    insert 
    into
        address
        (address_id, location) 
    values
        (null, ?)
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [VARCHAR] - [Made]
Hibernate: 
    insert 
    into
        contact
        (contact_id, address_address_id, name) 
    values
        (null, ?, ?)
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [BIGINT] - [1]
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [2] as [VARCHAR] - [Jan]
Hibernate: 
    select
        contact0_.contact_id as contact_1_2_0_,
        contact0_.address_address_id as address_3_2_0_,
        contact0_.name as name2_2_0_,
        address1_.address_id as address_1_0_1_,
        address1_.location as location2_0_1_ 
    from
        contact contact0_ 
    left outer join
        address address1_ 
            on contact0_.address_address_id=address1_.address_id 
    where
        contact0_.contact_id=?
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [BIGINT] - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([address_1_0_1_] : [BIGINT]) - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([address_3_2_0_] : [BIGINT]) - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([name2_2_0_] : [VARCHAR]) - [Jan]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([location2_0_1_] : [VARCHAR]) - [Made]
Hibernate: 
    select
        contact0_.contact_id as contact_1_2_1_,
        contact0_.address_address_id as address_3_2_1_,
        contact0_.name as name2_2_1_,
        address1_.address_id as address_1_0_0_,
        address1_.location as location2_0_0_ 
    from
        contact contact0_ 
    left outer join
        address address1_ 
            on contact0_.address_address_id=address1_.address_id 
    where
        contact0_.address_address_id=?
TRACE o.h.t.d.s.BasicBinder [main]: binding parameter [1] as [BIGINT] - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([address_1_0_0_] : [BIGINT]) - [1]
TRACE o.h.t.d.s.BasicExtractor [main]: extracted value ([contact_1_2_1_] : [BIGINT]) - [1]

Note: My EntityManagerFactoryCreator is a singleton that calls Persistence.createEntityManagerFactory("nl.infosupport.javaminor.week4.jpa.h2");

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">

  <persistence-unit name="nl.infosupport.javaminor.week4.jpa.h2">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value="sa"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/Documents/InfoSupport-Minor/h2_embedded_db/test"/>
      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>

      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
  </persistence-unit>

</persistence>

This is normal behavior in this case and with the tables config you have. If you want to have just one select, you need to use a custom query joining the child table. For example, using HQL it would look like that:

    public Contact find(Long id) {
        TypedQuery<Contact> query = em.createQuery(
        "SELECT c FROM Contact c join fetch c.address WHERE c.id = :id", Contact.class);
         return query
           .setParameter("id", id)
           .getSingleResult();
    }

The code is not necessary working, I didn't debug it, but it shows the principle.

UPDATED


Or you can try annotating the field like

@Fetch(FetchMode.JOIN)
private Address address;

And then it will fire just one query .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM