我有3个实体类:
@Table(name = "offer")
@DiscriminatorColumn(name="type")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Offer extends Audited {
...
@OneToMany(mappedBy="offer", fetch = FetchType.EAGER)
private Set<CompanyOffer> deals;
}
@Entity
@DiscriminatorValue("moving")
public class MovingOffer extends Offer {...}
@Table(name = "company_offer")
public class CompanyOffer extends Audited {
...
@NotNull
@ManyToOne
@JoinColumn(
name = "offer_id",
foreignKey = @ForeignKey(name = "fk_companyoffer_offer")
)
private Offer offer;
}
表格的创建完全符合预期,但当我想获得MovingOffer
时,则没有deals
。 deals属性始终设置为null
,但值正确存储在数据库中。
在hibernate的日志中,我可以找到以下行:
No collections were found in result set for role: com.company.entity.Offer.deals
可能出现问题的任何提示?
编辑:
为了获取数据,我使用JpaRepository。使用方法findFullById
public interface MovingOfferRepository extends JpaRepository<MovingOffer, UUID> {
@Query("SELECT mo FROM MovingOffer mo LEFT JOIN FETCH mo.deals WHERE mo.id = ?1")
MovingOffer findFullById(UUID movingOfferId);
}
以下是直接来自postgres的所有3个表的定义:
Table "public.offer"
Column | Type | Modifiers | Storage | Stats target | Description
type | character varying(31) | not null | extended | |
id | uuid | not null | plain | |
createdat | timestamp without time zone | not null | plain | |
lastmodifiedat | timestamp without time zone | not null | plain | |
deliverydate | bytea | | extended | |
externalorderid | character varying(255) | | extended | |
status | character varying(255) | not null | extended | |
customer_id | uuid | | plain | |
Indexes:
"offer_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk_offer_customer" FOREIGN KEY (customer_id) REFERENCES customer(id)
Referenced by:
TABLE "company_offer" CONSTRAINT "fk_companyoffer_offer" FOREIGN KEY (offer_id) REFERENCES offer(id)
TABLE "movingoffer" CONSTRAINT "fkg74slpkg2tob4b3ydqilcfdrn" FOREIGN KEY (id) REFERENCES offer(id)
Table "public.movingoffer"
Column | Type | Modifiers | Storage | Stats target | Description
------------------+---------+-----------+---------+--------------+-------------
boxcount | integer | | plain | |
id | uuid | not null | plain | |
startaddress_id | uuid | not null | plain | |
targetaddress_id | uuid | not null | plain | |
Indexes:
"movingoffer_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk_address_from" FOREIGN KEY (startaddress_id) REFERENCES address(id)
"fk_address_to" FOREIGN KEY (targetaddress_id) REFERENCES address(id)
"fkg74slpkg2tob4b3ydqilcfdrn" FOREIGN KEY (id) REFERENCES offer(id)
Table "public.company_offer"
Column | Type | Modifiers | Storage | Stats target | Description
-------------------+-----------------------------+-----------+----------+--------------+-------------
id | uuid | not null | plain | |
createdat | timestamp without time zone | not null | plain | |
lastmodifiedat | timestamp without time zone | not null | plain | |
estimatedduration | double precision | | plain | |
message | text | | extended | |
totalprice | numeric(19,2) | | main | |
company_id | uuid | not null | plain | |
offer_id | uuid | not null | plain | |
Indexes:
"company_offer_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk_companyoffer_company" FOREIGN KEY (company_id) REFERENCES company(id)
"fk_companyoffer_offer" FOREIGN KEY (offer_id) REFERENCES offer(id)