onetomany懒惰取hibernate essue

时间:2017-05-02 14:18:02

标签: java hibernate

Hy,我有懒惰的提问问题,并且为什么会发生这种问题。 我有两个问题:

@Entity
@Table(name = "PERSONS")
public class Persons {

    ...

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public List<Cards> cards = new ArrayList<Cards>();

    ...
}

@Entity
@Table(name = "CARDS")
public class Cards {

    ...

    @ManyToOne (fetch = FetchType.EAGER)
    @JoinColumn(name = "PERSON_ID")
    @JsonBackReference
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Persons person;

    ...
}

和人的存储库:

@Repository
@Transactional
public class PersonRepository {
    @Autowired
    private SessionFactory sessionFactory;
    public List<Persons> getAll() {
        return getSession().createCriteria(Persons.class).list();
    }
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }
}

当我尝试获取人物时,我也会为每个人收集所有卡片,但我不想收集这些卡片。我怎么能解决这个问题?

SQL查询:

    select
        this_.PERSON_ID as PERSON_ID1_10_2_,
        this_.CITY_ID as CITY_ID8_10_2_,
        this_.EMAIL as EMAIL2_10_2_,
        this_.FIRST_NAME as FIRST_NAME3_10_2_,
        this_.LAST_NAME as LAST_NAME4_10_2_,
        this_.MOBILE_NUMBER as MOBILE_NUMBER5_10_2_,
        this_.NICKNAME as NICKNAME6_10_2_,
        this_.PASSWORD as PASSWORD7_10_2_,
        this_.ROLE_ID as ROLE_ID9_10_2_,
        cities2_.CITY_ID as CITY_ID1_5_0_,
        cities2_.CITY_NAME as CITY_NAME2_5_0_,
        rollers3_.ROLE_ID as ROLE_ID1_11_1_,
        rollers3_.ROLE_TYPE as ROLE_TYPE2_11_1_ 
    from
        PERSONS this_ 
    inner join
        CITIES cities2_ 
            on this_.CITY_ID=cities2_.CITY_ID 
    left outer join
        ROLLERS rollers3_ 
            on this_.ROLE_ID=rollers3_.ROLE_ID
Hibernate: 
    select
        this_.PERSON_ID as PERSON_ID1_10_2_,
        this_.CITY_ID as CITY_ID8_10_2_,
        this_.EMAIL as EMAIL2_10_2_,
        this_.FIRST_NAME as FIRST_NAME3_10_2_,
        this_.LAST_NAME as LAST_NAME4_10_2_,
        this_.MOBILE_NUMBER as MOBILE_NUMBER5_10_2_,
        this_.NICKNAME as NICKNAME6_10_2_,
        this_.PASSWORD as PASSWORD7_10_2_,
        this_.ROLE_ID as ROLE_ID9_10_2_,
        cities2_.CITY_ID as CITY_ID1_5_0_,
        cities2_.CITY_NAME as CITY_NAME2_5_0_,
        rollers3_.ROLE_ID as ROLE_ID1_11_1_,
        rollers3_.ROLE_TYPE as ROLE_TYPE2_11_1_ 
    from
        PERSONS this_ 
    inner join
        CITIES cities2_ 
            on this_.CITY_ID=cities2_.CITY_ID 
    left outer join
        ROLLERS rollers3_ 
            on this_.ROLE_ID=rollers3_.ROLE_ID
Result set row: 0

` 我用sql查询做错了。

我在appcontroller类中配置logger:

@RequestMapping(value = {"/", "/home"}, method = RequestMethod.GET)
public String homePage(ModelMap model) {
    model.addAttribute("greeting", "Welcome to the first page of the project");
    List<Persons> personsList = personService.getUsers();
    if (Const.DEBUG) {
        if (logger.isDebugEnabled()) {
            for (Persons persons : personsList) {
                logger.debug("Array Card size " + persons.getCards().size() + " for " + persons.getNickname());
                for (Cards card : persons.getCards()) {
                    logger.debug("CardName " + card.getCardName());
                }
            }
        }
    }
    return "welcome";
}

然后在调试模式下运行项目我在控制台输出文件中得到它:

[DEBUG] 2017-05-02 08:32:46 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for Alex
[DEBUG] 2017-05-02 08:32:49 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for ui1
[DEBUG] 2017-05-02 08:32:50 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for ui2
[DEBUG] 2017-05-02 08:32:50 com.team.mvc.controller.AppController:homePage:55 - Array Card size 1 for vvv
[DEBUG] 2017-05-02 08:32:51 com.team.mvc.controller.AppController:homePage:57 - CardName null
[DEBUG] 2017-05-02 08:32:53 com.team.mvc.controller.AppController:homePage:55 - Array Card size 1 for Sam
[DEBUG] 2017-05-02 08:32:54 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for Alex
[DEBUG] 2017-05-02 08:32:54 com.team.mvc.controller.AppController:homePage:57 - CardName null
[DEBUG] 2017-05-02 08:32:55 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for Alex
[DEBUG] 2017-05-02 08:32:56 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for ui1
[DEBUG] 2017-05-02 08:32:56 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for ui1
[DEBUG] 2017-05-02 08:32:56 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for 7
[DEBUG] 2017-05-02 08:32:56 com.team.mvc.controller.AppController:homePage:55 - Array Card size 0 for ui2

这意味着用户&#34; vvv&#34;有一张卡片和懒人取物不能正常工作。

Class PersonService:

    @Service
    @Transactional
    public class PersonService {

        ...

        @Autowired
        PersonRepository personRepository;

        public List<Persons> getUsers() {
            List<Persons> personList = new ArrayList<>();
            for (Persons person : personRepository.getAll()) {
                if (person.getRole().getRoleType().equals("USER"))
                    personList.add(person);
            }
            return personList;
        }

        ...
    }

我错在哪里?

0 个答案:

没有答案