//Repository
package com.thekhuc.repository;
import java.util.List;
import com.thekhuc.model.Goal;
import com.thekhuc.model.GoalReport;
public interface GoalRepository {
Goal save(Goal goal);
List<Goal> loadAll();
List<GoalReport> findAllGoalReports();
}
//Interface
package com.thekhuc.repository;import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import com.thekhuc.model.Goal;
import com.thekhuc.model.GoalReport;
@Repository("goalRepository")
public class GoalRepositoryImpl implements GoalRepository {
@PersistenceContext
private EntityManager em;
@Override
public Goal save(Goal goal) {
if(goal.getId() == null){
em.persist(goal);
em.flush();
}
else{
goal = em.merge(goal);
}
return goal;
}
@Override
public List<Goal> loadAll() {
//Query query=em.createQuery("Select g from Goal g");
TypedQuery<Goal> query = em.createNamedQuery(Goal.FIND_ALL_GOALS, Goal.class);
return query.getResultList();
}
@Override
public List<GoalReport> findAllGoalReports() {
//Query query = em.createQuery("Select new com.thekhuc.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();
}
}
No comments:
Post a Comment