Я действительно не могу присоединиться к своей сущности Address с моей сущностью AddressProperty.Я хочу получить объект Address со всеми полями AddressProperty, но поля 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 TRACE 15740 --- [main] ohtype.descriptor.sql.BasicBinder: параметр привязки [1] как [BIGINT] - [234]
Мой вопрос - ПОЧЕМУ параметры привязки 234?Я получаю объект Address с AddressProperty как NULL.Как соединить мою сущность Address с AddressProperty по полю idAddress?
Я надеюсь, что это не ошибка Hibernate: https://hibernate.atlassian.net/browse/HHH-7307
Я использовал очень много опций (прокомментировал в моем коде) но безуспешно.