如何解决具有延迟加载的复合PK似乎不起作用
我正在缓慢而稳步地走向绝望..我有一个简单的案例,其中包含使用复合键的联接/关系表,并试图获取一个具有联接表信息的实体,但是对于其他情况,我正在寻找延迟加载。来了:
CREATE TABLE `recipes` ( `id` bigint(20) NOT NULL );
CREATE TABLE `recipe_ingredients` ( `recipe_id` bigint(20) NOT NULL,`ingredient_id` bigint(20) NOT NULL,`amount` INT NULL,PRIMARY KEY (`recipe_id`,`ingredient_id`);
CREATE TABLE `ingredients` ( `id` bigint(20) NOT NULL,`name` longtext NOT NULL );
出于长度考虑,省略了FK约束。 现在在代码中,我有:
@Entity
@Table(name = "recipes")
@Getter
@Setter
@NoArgsConstructor
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class Recipe implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
@OneToMany(
mappedBy = "recipe",orphanRemoval = true,fetch = FetchType.LAZY,cascade = CascadeType.PERSIST
)
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
private Set<RecipeIngredient> ingredients = new HashSet<RecipeIngredient>();
}
成分类
@Entity
@Table(name="ingredients")
@Getter
@Setter
@NoArgsConstructor
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class Ingredient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Lob
private String name;
@OneToMany(mappedBy="ingredient",fetch = FetchType.LAZY)
private Set<RecipeIngredient> recipes = new HashSet<RecipeIngredient>();
}
RecipeIngredientId:
@NoArgsConstructor
@Getter @Setter
public class RecipeIngredientId implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Recipe recipe;
private Ingredient ingredient;
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
RecipeIngredientId that = (RecipeIngredientId) o;
return Objects.equals( ingredient.getId(),that.ingredient.getId() ) &&
Objects.equals( recipe.getId(),that.recipe.getId() );
}
@Override
public int hashCode() {
return Objects.hash( recipe.getId(),ingredient.getId() );
}
public RecipeIngredientId(Recipe recipe2,Ingredient ingredient2) {
this.recipe = recipe2;
this.ingredient = ingredient2;
}
}
最后是交叉表:
@Entity
@Table(name="recipe_ingredients")
@Getter
@Setter
@NoArgsConstructor
@IdClass(RecipeIngredientId.class)
public class RecipeIngredient implements Serializable {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
private Recipe recipe;
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
private Ingredient ingredient;
private int amount;
}
在RecipeRepositoryImpl中:
TypedQuery<Object> query = em.createQuery("SELECT r FROM Recipe r LEFT JOIN r.ingredients ri"
+ " WHERE r.id= :id",Object.class);
query.setParameter("id",id);
return query.getResultList();
尽管我现在疯狂地将Lazy放在任何地方,但我仍在获取所有数据-仍对配料表的查询更加轻松。即:
{
"id": 1,"ingredients": [
{
"recipe": 1,"ingredient": {
"id": 1,"name": "Kartoffeln"
"recipes": [
{
"recipe": 1,"ingredient": 1,"amount": 100
}
]
},"amount": 100
}
]
}
]
我希望成分再次仅包含ID字段,而不包含名称和循环配方列表... 感谢您的帮助! 干杯,尼基
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。