java NamedQuery(通常在域对象中定义)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java NamedQuery(通常在域对象中定义)相关的知识,希望对你有一定的参考价值。
package com.pluralsight.repository;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import com.pluralsight.model.Goal;
import com.pluralsight.model.GoalReport;
@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository {
@PersistenceContext
private EntityManager em;
public Goal save(Goal goal) {
em.persist(goal);
// has to call flush to save into database
em.flush();
return goal;
}
public List<Goal> loadAll() {
// Query query = em.createQuery("Select g from Goal g");
TypedQuery<Goal> query = em.createNamedQuery(Goal.FIND_ALL_GOALS,Goal.class);
List goals = query.getResultList();
return goals;
}
public List<GoalReport> findAllGoalReports() {
// Query query = em.createQuery("Select new com.pluralsight.model.GoalReport(g.minutes, e.minutes, e.activity)"
// + "from Goal g, Exercise e where g.id = e.goal.id");
TypedQuery<GoalReport> query = em.createNamedQuery(Goal.FIND_GOAL_REPORTS, GoalReport.class);
return query.getResultList();
}
}
package com.pluralsight.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Range;
@Entity
@Table(name="goals")
@NamedQueries({
@NamedQuery(name=Goal.FIND_GOAL_REPORTS, query="Select new com.pluralsight.model.GoalReport(g.minutes, e.minutes, e.activity)"
+ "from Goal g, Exercise e where g.id = e.goal.id"),
@NamedQuery(name=Goal.FIND_ALL_GOALS, query="Select g from Goal g")
})
public class Goal {
public static final String FIND_GOAL_REPORTS = "findGoalReports";
public static final String FIND_ALL_GOALS = "findAllGoals";
@Id
@GeneratedValue
@Column(name="GOAL_ID")
private Long id;
@Range(min = 1, max = 120)
@Column(name="MINUTES")
private int minutes;
@OneToMany(mappedBy="goal", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Exercise> exercises = new ArrayList<Exercise>();
public List<Exercise> getExercises() {
return exercises;
}
public void setExercises(List<Exercise> exercises) {
this.exercises = exercises;
}
public Long getId() {
return id;
}
public int getMinutes() {
return minutes;
}
public void setId(Long id) {
this.id = id;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
}
以上是关于java NamedQuery(通常在域对象中定义)的主要内容,如果未能解决你的问题,请参考以下文章
@NamedQuery : FROM 子句中未定义标识变量“sysdate”
在 Hibernate 的 NamedQuery 中使用用户定义的变量
如何在 JPA 中使用 NamedQuery
使用 Int 的 Java jSF NamedQuery
从 JPA 实体的接口中检索 @NamedQuery
如何向 NamedQuery/MappedSelect 添加预取?