0

我试图面对一项服务的一些问题,它应该返回用户数据+所有收件人的列表(分配给用户 ID),但我得到的结果只是用户数据,没有收件人:

person_id: 3,
firstName: "Marek",
lastName: "Bawski",
email: "Pavel@karl.com",
password: "##@$#FDSgsdG",
address: {
street: "Street",
houseNumber: 4,
apartmentNumber: 0,
zipCode: "13-200",
province: "XSXSXSX"
}

hibernate 生成的 SQL:

Hibernate: 
    select
        person0_.person_id as person_i1_2_0_,
        person0_.apartment_number as apartmen2_2_0_,
        person0_.house_number as house_nu3_2_0_,
        person0_.province as province4_2_0_,
        person0_.street as street5_2_0_,
        person0_.zip_code as zip_code6_2_0_,
        person0_.email as email7_2_0_,
        person0_.firstName as firstNam8_2_0_,
        person0_.lastName as lastName9_2_0_,
        person0_.user_pwd as user_pw10_2_0_ 
    from
        Person person0_ 
    where
        person0_.person_id=?
Hibernate: 
    select
        recipe0_.recipe_id as recipe_i1_4_,
        recipe0_.aprox_price as aprox_pr2_4_,
        recipe0_.calories as calories3_4_,
        recipe0_.calories_cat_id as calories4_4_,
        recipe0_.person_id as person_i9_4_,
        recipe0_.raiting as raiting5_4_,
        recipe0_.recipe_name as recipe_n6_4_,
        recipe0_.recipe_cat_id as recipe_c7_4_,
        recipe0_.weight as weight8_4_ 
    from
        recipe_table recipe0_ 
    left outer join
        Person person1_ 
            on recipe0_.person_id=person1_.person_id 
    where
        person1_.person_id=?
Hibernate: 
    select
        person0_.person_id as person_i1_2_0_,
        person0_.apartment_number as apartmen2_2_0_,
        person0_.house_number as house_nu3_2_0_,
        person0_.province as province4_2_0_,
        person0_.street as street5_2_0_,
        person0_.zip_code as zip_code6_2_0_,
        person0_.email as email7_2_0_,
        person0_.firstName as firstNam8_2_0_,
        person0_.lastName as lastName9_2_0_,
        person0_.user_pwd as user_pw10_2_0_ 
    from
        Person person0_ 
    where
        person0_.person_id=?
Hibernate: 
    select
        components0_.recipe_id as recipe_i6_1_0_,
        components0_.component_id as componen1_1_0_,
        components0_.component_id as componen1_1_1_,
        components0_.component_description as componen2_1_1_,
        components0_.component_kcal as componen3_1_1_,
        components0_.component_name as componen4_1_1_,
        components0_.component_weight as componen5_1_1_,
        components0_.recipe_id as recipe_i6_1_1_ 
    from
        component components0_ 
    where
        components0_.recipe_id=?

主要实体类 - 配方有关系:

@Entity
@Table(name="recipe_table")
public class Recipe{
    @ManyToOne() 
    @JoinColumn(name="person_id")
    @JsonManagedReference
    private Person person; //getters/setters + other fields

}

其次,这是 Person 实体:

@Entity
public class Person  {

 @OneToMany(mappedBy = "person",fetch = FetchType.EAGER))
 @JsonBackReference
 private List<Recipe> recipes;//getters/setters+other fields

以及获取用户所有食谱的方法,看起来:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Person getUserRecipeWithComponents(long id) {
    Person person = findOne(id);
    List<Recipe> recipes = recipeRepository.findByPerson(person);
    person.setRecipes(recipes);

    return person;
}

在由 JpaRepository 扩展的 RecipeRepository 中:

List<Recipe> findByPerson(Person person);

还有一件奇怪的事情,我没想到我创建了简单的方法来检索一个食谱(只有食谱数据):

 @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Recipe findRecipeById(long id){
        Recipe recipe = recipeRepository.findOne(id);

        return recipe;
    }

在此之后,我得到了包含用户数据的食谱数据:

{
recipe_id: 3,
recipeName: "Pasta",
weight: 453,
calories: 1500,
aprox_price: 12,
raiting: 7,
person: {
person_id: 3,
firstName: "Marek",
lastName: "Bawski",
email: "Pawel@karol.pl",
password: "Karol123",
address: {
street: "Polna",
houseNumber: 4,
apartmentNumber: 0,
zipCode: "13-200",
province: "Pomorskie"
}
},
calories_cat_id: 1,
recipe_cat_id: 2
}

hibernate生成的SQL:

Hibernate: 
    select
        recipe0_.recipe_id as recipe_i1_4_0_,
        recipe0_.aprox_price as aprox_pr2_4_0_,
        recipe0_.calories as calories3_4_0_,
        recipe0_.calories_cat_id as calories4_4_0_,
        recipe0_.person_id as person_i9_4_0_,
        recipe0_.raiting as raiting5_4_0_,
        recipe0_.recipe_name as recipe_n6_4_0_,
        recipe0_.recipe_cat_id as recipe_c7_4_0_,
        recipe0_.weight as weight8_4_0_,
        components1_.recipe_id as recipe_i6_1_1_,
        components1_.component_id as componen1_1_1_,
        components1_.component_id as componen1_1_2_,
        components1_.component_description as componen2_1_2_,
        components1_.component_kcal as componen3_1_2_,
        components1_.component_name as componen4_1_2_,
        components1_.component_weight as componen5_1_2_,
        components1_.recipe_id as recipe_i6_1_2_,
        person2_.person_id as person_i1_2_3_,
        person2_.apartment_number as apartmen2_2_3_,
        person2_.house_number as house_nu3_2_3_,
        person2_.province as province4_2_3_,
        person2_.street as street5_2_3_,
        person2_.zip_code as zip_code6_2_3_,
        person2_.email as email7_2_3_,
        person2_.firstName as firstNam8_2_3_,
        person2_.lastName as lastName9_2_3_,
        person2_.user_pwd as user_pw10_2_3_ 
    from
        recipe_table recipe0_ 
    left outer join
        component components1_ 
            on recipe0_.recipe_id=components1_.recipe_id 
    left outer join
        Person person2_ 
            on recipe0_.person_id=person2_.person_id 
    where
        recipe0_.recipe_id=?

我搞砸了注释吗?

我会很感激你的帮助!

4

0 回答 0