我是学生,是Stack Overflow,spring-boot和hibernate stack的新手。
问题:根据类别ID返回问题列表。
由于我的模型实体类在下面,我没有categoryId(外键)的属性,因为这是通过关系注释自动生成的。
我似乎能够返回所有问题的列表,没有过滤级别,但也想要一个根据类别ID(外键)返回问题的方法。
非常感谢任何帮助,谢谢!
使用hibernate 5.2
categoryModel
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min = 3, max = 20)
private String name;
@NotNull
@Pattern(regexp = "#[0-9a-fA-F]{6}")
private String colorCode;
@OneToMany(mappedBy = "category")
private List<Question> questions = new ArrayList<>();
public Category(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Question> getQuestions() {
return questions;
}
public String getColorCode() {
return colorCode;
}
public void setColorCode(String colorCode) {
this.colorCode = colorCode;
}
questionModel
@Entity
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
private Category category;
private LocalDateTime dateCreated = LocalDateTime.now();
public Question(){}
public String getTimeSinceUploaded() {
String unit = "";
LocalDateTime now = LocalDateTime.now();
long diff;
if((diff = ChronoUnit.SECONDS.between(dateCreated,now)) < 60){
unit = "secs";
} else if ((diff = ChronoUnit.MINUTES.between(dateCreated,now)) < 60) {
unit = "mins";
} else if ((diff = ChronoUnit.HOURS.between(dateCreated,now)) < 24) {
unit = "hours";
} else if ((diff = ChronoUnit.DAYS.between(dateCreated,now)) < 30) {
unit = "days";
} else if ((diff = ChronoUnit.MONTHS.between(dateCreated,now)) < 12) {
unit = "months";
} else{
diff = ChronoUnit.YEARS.between(dateCreated,now);
}
return String.format("%d %s",diff,unit);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public LocalDateTime getDateCreated() {
return dateCreated;
}
public void setDateCreated(LocalDateTime dateCreated) {
this.dateCreated = dateCreated;
}
}
问题DAO
@Repository
public class QuestionDaoImpl implements IQuestionDao {
@Autowired
private SessionFactory sessionFactory;
/**
* Returns a list of all the questions - michael
*/
@Override
public List<Question> findAll() {
Session session = sessionFactory.openSession();
// Create CriteriaBuilder
CriteriaBuilder builder = session.getCriteriaBuilder();
// Create CriteriaQuery
CriteriaQuery<Question> criteria = builder.createQuery(Question.class);
// Specify criteria root
criteria.from(Question.class);
// Execute query
List<Question> questions = session.createQuery(criteria).getResultList();
session.close();
return questions;
}
/**
* Returns A question by using the id - michael
*/
@Override
public Question findById(Long id) {
Session session = sessionFactory.openSession();
Question question = session.get(Question.class, id);
session.close();
return question;
}
/**
* TODO: ml- Finds a list of questions according to the category_id
*/
@Override
public List<Question> findByCategoryId(Long categoryId){
// This is a issue i a have here
return null;
}
答案 0 :(得分:0)
试试这个,你必须传递你创建的对象问题实体的类别对象bcz,
@Override
public List<Question> findByCategoryId(Long categoryId){
Session session = sessionFactory.openSession();
final Criteria cr = session.createCriteria(Question.class);
cr.add(Restrictions.eq("category", category));
return cr.list();
}
如果有任何问题,请告诉我。
答案 1 :(得分:0)
您的Dao是否扩展了crudrepository或JpaRepository?