Mam następujące jednostki, item
, które mogą mieć maksymalnie dwie kategorie: podstawową i dodatkową. Obie kategorie są mapowane ManyToOne
na tabelę category
przy użyciu JoinColumnsOrFormulas
. Pierwszy pobiera się zgodnie z oczekiwaniami, ale drugi nie występuje w instrukcji SQL i jest leniwy. To leniwy ładunek skutkuje klasycznym problemem n + 1.Hibernuj dwie relacje ManyToOne na jednej tabeli, pierwsza dostaje ochotę, a druga LAZY jest załadowana
To moja jednostka element z obu podmiotów kategorii, które powinny dostaje Rejestracja:
@Entity
@Table(name = "item", schema = "public", catalog = "stackoverflow_question")
@DynamicUpdate
public class Item extends StackOverflowQuestionEntity {
@Id
@Column(name = "id")
protected Long id;
@Column(name = "site")
private String site;
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "site", referencedColumnName = "site")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "primary_category_id", referencedColumnName = "category_id"))
})
private Category primaryCategory;
@Column(name = "primary_category_id")
private Long primaryCategoryId;
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "site", referencedColumnName = "site")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "secondary_category_id", referencedColumnName = "category_id"))
})
private Category secondaryCategory;
@Column(name = "secondary_category_id")
private Long secondaryCategoryId;
}
Jest to jednostka kategoria:
@Entity
@Table(name = "category", schema = "public", catalog = "stackoverflow_question")
public class Category extends StackOverflowQuestionEntity {
@Column(name = "category_id")
private Long categoryId;
@Column(name = "name")
private String name;
@Column(name = "site")
private String site;
}
Powstały zapytanie zawiera jedynie kategoria podstawowy:
SELECT this_.id AS id1_9_9_,
this_.inserted AS inserted2_9_9_,
this_.updated AS updated3_9_9_,
this_.primary_category_id AS formula174_9_,
this_.secondary_category_id AS formula176_9_,
category2_.id AS id1_0_0_,
category2_.inserted AS inserted2_0_0_,
category2_.updated AS updated3_0_0_,
category2_.name AS name7_0_0_
FROM public.item this_
LEFT OUTER JOIN public.category category2_ ON this_.site=category2_.site
AND this_.primary_category_id=category2_.category_id
WHERE True;
W związku z tym do drugiej kategorii dołącza leniwy:
SELECT category0_.id AS id1_0_0_,
category0_.inserted AS inserted2_0_0_,
category0_.updated AS updated3_0_0_,
category0_.name AS name4_0_0_,
category0_.site AS site5_0_0_
FROM public.category category0_
WHERE category0_.site=?
AND category0_.category_id=?;
Dlaczego Hibernate dołącza do leniwej kategorii drugorzędnej, adnotacje wydają się być takie same.
Wersja hibernacji, której używam, to 5.0.10.Final.
ten sposób jednostka bazowa wygląda następująco:
@MappedSuperclass
abstract public class StackOverflowQuestionEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, insertable = true, updatable = false, nullable = false)
protected Long id;
@Type(type="LocalDateTime")
@Column(name = "created", nullable = false, insertable = true, updatable = false)
protected LocalDateTime created;
@Type(type="LocalDateTime")
@Column(name = "refreshed", nullable = false, insertable = true, updatable = true)
protected LocalDateTime refreshed;
@PreUpdate
protected void onUpdate() {
refreshed = now();
}
@PrePersist
protected void onCreate() {
created = refreshed = now();
}
}
Oto przykład „zapytanie”, jak powiedział używam hibernacji kryteria, jak HQL, problem pojawia się z obu metod.
session
.createCriteria(Item.class)
.add(eq("id", id))
.uniqueResult();
spróbować użyć standardowych @JoinColumns JPA .. zamiast hibernacji jeden –
Wszelkich szczegółów w modelu bazy danych? I na przykład druga kategoria nullable? – Gimby
Obie identyfikatory kategorii są zerowalne w modelu bazy danych: \ d pozycja primary_category_id | bigint secondary_category_id | bigint –