Hibernate OneToOne不按列联接-忽略引用的列名

时间:2019-03-02 23:14:45

标签: hibernate join one-to-one mappedby

我真的不能将我的地址实体与我的AddressProperty实体一起加入。我想获取具有所有AddressProperty字段的Address对象,但Addressproperty字段为NULL:

地址: 包com.hibernate.hibernate.entity;

import java.io.Serializable;
import javax.persistence.*;


@Entity
@Table(name = "adres")
public class Address implements Serializable {

    private static final long serialVersionUID = 8056475486223191148L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id_adres", nullable = false)
    private Long idAddress;

    @Column(name = "id_klient")
    private Long idClient;


    @Column(name = "id_gmina")
    private Long idCommune;

    @Column(name = "id_powiat")
    private Long idDistrict;

    @Column(name = "id_wojewodztwo")
    private Long idProvince;

    //  @OneToOne(mappedBy = "idAddress")
//  @JoinColumn(name = "id_adres", referencedColumnName = "id_adres")
//  @OneToOne
//  @PrimaryKeyJoinColumn
//  @OneToOne(mappedBy = "idAddress")
    @OneToOne
    @JoinColumn(name = "id_adres", referencedColumnName = "id_adres")
    private AddressProperty addressProperty;

    public Long getIdAddress() {
        return idAddress;
    }

    public void setIdAddress(Long idAddress) {
        this.idAddress = idAddress;
    }

    public Long getIdClient() {
        return idClient;
    }

    public void setIdClient(Long idClient) {
        this.idClient = idClient;
    }

    public Long getIdCommune() {
        return idCommune;
    }

    public void setIdCommune(Long idCommune) {
        this.idCommune = idCommune;
    }

    public Long getIdDistrict() {
        return idDistrict;
    }

    public void setIdDistrict(Long idDistrict) {
        this.idDistrict = idDistrict;
    }

    public Long getIdProvince() {
        return idProvince;
    }

    public void setIdProvince(Long idProvince) {
        this.idProvince = idProvince;
    }

    public AddressProperty getAddressProperty() {
        return addressProperty;
    }

    public void setAddressProperty(AddressProperty addressProperty) {
        this.addressProperty = addressProperty;
    }
}

和AddressProperty实体:

package com.hibernate.hibernate.entity;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name = "adres_property")
public class AddressProperty implements Serializable {

    private static final long serialVersionUID = 8056422486533191148L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "id_adres", nullable = false)
    private Long idAddress; 

    @Column(name = "zablokowana_edycja")
    private Boolean blockedUpdate;

    //  @OneToOne(optional = false)
//  @JoinColumn(name = "id_adres", insertable = false, updatable = false, unique = true)
    @OneToOne(mappedBy = "addressProperty")
    private Address address;

    public Long getId() {
        return id;
    }

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

    public Long getIdAddress() {
        return idAddress;
    }

    public void setIdAddress(Long idAddress) {
        this.idAddress = idAddress;
    }

    public Boolean getBlockedUpdate() {
        return blockedUpdate;
    }

    public void setBlockedUpdate(Boolean blockedUpdate) {
        this.blockedUpdate = blockedUpdate;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

以下是两个对应的表: 表地址:

CREATE TABLE adres (

        id_adres bigserial NOT NULL,
        id_klient int8 NULL,
        id_wojewodztwo int8 NULL,
        id_powiat int8 NULL,
        id_gmina int8 NULL,
        CONSTRAINT pk_adres PRIMARY KEY (id_adres)
    )

带有示例值:

id_adres    id_klient   id_wojewodztwo  id_powiat   id_gmina
234         2000002     1               1           1
235         2000002     2               2           2

还有adres_property:

CREATE TABLE adres_property (
    id bigserial NOT NULL,
    id_adres int8 NULL,
    zablokowana_edycja bool NULL,
    CONSTRAINT adres_property_pkey PRIMARY KEY (id),
    CONSTRAINT adres_property_id_adres_fkey FOREIGN KEY (id_adres) REFERENCES adres(id_adres)
)

带有示例值:

id  id_adres    zablokowana_edycja
1   234         true
2   235         false

当我要执行查询时:

public List<Address> getAddressByClientId(Long clientId){
        List<Address> address = null;
        try {
            address = em.createQuery("SELECT b FROM Address b WHERE b.idClient=:id").setParameter("id", clientId).getResultList();

        } catch (Exception e) {
            System.out.println("ERROR!" + e);
        }
        return address;

Hibernate像这样创建sql查询:

    select
        address0_.id_adres as id_adres1_0_,
        address0_.id_klient as id_klien2_0_,
        address0_.id_gmina as id_gmina3_0_,
        address0_.id_powiat as id_powia4_0_,
        address0_.id_wojewodztwo as id_wojew5_0_ 
    from
        adres address0_ 
    where
        address0_.id_klient=?
2019-03-03 00:24:21.465 TRACE 16052 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [2000002]

和:

select
        addresspro0_.id as id1_1_0_,
        addresspro0_.zablokowana_edycja as zablokow2_1_0_,
        addresspro0_.id_adres as id_adres3_1_0_,
        address1_.id_adres as id_adres1_0_1_,
        address1_.id_klient as id_klien2_0_1_,
        address1_.id_gmina as id_gmina3_0_1_,
        address1_.id_powiat as id_powia4_0_1_,
        address1_.id_wojewodztwo as id_wojew5_0_1_ 
    from
        adres_property addresspro0_ 
    left outer join
        adres address1_ 
            on addresspro0_.id=address1_.id_adres 
    where
        addresspro0_.id=?

2019-03-02 23:58:10.776跟踪15740 --- [main] ohtype.descriptor.sql.BasicBinder:将参数[1]绑定为[BIGINT]-[234]

我的问题是-为什么绑定参数是234? 我得到AddressProperty为NULL的Address对象。 如何通过idAddress字段将我的Address实体与AddressProperty结合在一起?

我希望这不是像这样的Hibernate BUG: https://hibernate.atlassian.net/browse/HHH-7307

我使用了很多选项(在代码中有注释),但没有成功。

0 个答案:

没有答案