使用springboot,和mongo的repository,我定義了一個Comparator類,想實現自己的對象的比較方法。代碼如下:
package com.story.utils;
import java.util.Comparator;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.story.model.Phase;
import com.story.model.Story;
import com.story.repository.StoryRepository;
@Service
public class PhaseComparator implements Comparator<Phase>{
private String field;
private Story story;
@Autowired
private StoryRepository storyRepository;
public PhaseComparator() {
super();
}
public PhaseComparator (String field) {
this.field = field;
}
@Override
public int compare(Phase phase_1, Phase phase_2) {
if (this.field.equals("createdDate")) {
return phase_1.getCreatedDate() < phase_2.getCreatedDate() ? -1 : 1;
} else {
Story foundStory_1 = this.storyRepository.findOne(phase_1.getStoryId());
Story foundStory_2 = this.storyRepository.findOne(phase_1.getStoryId());
return foundStory_1.getLastUpdatedDate() < foundStory_2.getLastUpdatedDate() ? -1 : 1;
}
}
}
但是,這樣的話,storyRepository就是null。請問應該如何處理呢?謝謝
添加回答
舉報
0/150
提交
取消
