OneToOne Find上的JPA / Hibernate双选

时间:2017-09-20 19:09:48

标签: java hibernate jpa

在学习JPA / Hibernate时,我发现了意想不到的事情。我得到一个不必要的双选择查询到db(请参阅buttom)。我有一个简单的OneToOne设置。

这是我的联系实体:

@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

}

我的地址实体:

@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

}

这就是我运行代码的方式:

  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);
  }

}

这可能是愚蠢的,但导致双重选择的原因是什么?

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]

注意: 我的EntityManagerFactoryCreator是一个调用的单例 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>

1 个答案:

答案 0 :(得分:1)

在这种情况下,这是正常行为,并且您具有表配置。如果您只想选择一个,则需要使用加入子表的自定义查询。例如,使用HQL看起来像:

    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();
    }

代码没有必要工作,我没有调试它,但它显示了原理。

<强>已更新

或者您可以尝试注释该字段,如

@Fetch(FetchMode.JOIN)
private Address address;

And then it will fire just one query