我的Java编码班的老师在他的网页上发布了一个他希望我们为自己编写代码的例子。但是,当我尝试在自己的实验室中重新创建相同的代码时,我遇到了一些错误。这是代码:
public class StudentRepositoryImpl extends Repository<Student, Long> implements StudentRepositoryCustom {
private static final Logger log = LoggerFactory.getLogger(StudentRepositoryImpl.class);
public StudentRepositoryImpl()
{
super(Student.class);
}
@Override
@Transactional
public List<Student> findAllWithLabsSqlQuery()
{
log.trace("findAllWithLabsSqlQuery: method entered");
HibernateEntityManager hibernateEntityManager = getEntityManager().unwrap(HibernateEntityManager.class);
Session session = hibernateEntityManager.getSession();
Query query = session.createSQLQuery("select distinct {s.*}, {sd.*}, {d.*}" +
" from student s" +
" left join student_lab sd on sd.student_id = s.id" +
" left join lab d on d.id = sd.lab_id")
.addEntity("s", Student.class)
.addJoin("sd", "s.studentLabs")
.addJoin("d", "sd.lab")
.addEntity("s", Student.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Student> students = query.list();
log.trace("findAllWithLabsSqlQuery: students={}", students);
return students;
}
@Override
@Transactional
public List<Student> findAllWithLabsJpql() {
log.trace("findAllWithLabsJpql: method entered");
javax.persistence.Query query = getEntityManager().createQuery("select distinct s from Student s" +
" left join fetch s.studentLabs sd" +
" left join fetch sd.lab d");
List<Student> students = query.getResultList();
log.trace("findAllWithLabsJpql: students={}", students);
return students;
}
@Override
@Transactional
public List<Student> findAllWithLabsJpaCriteria() {
log.trace("findAllWithLabsJpaCriteria: method entered");
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Student> query = criteriaBuilder.createQuery(Student.class);
query.distinct(Boolean.TRUE);
Root<Student> from = query.from(Student.class);
Fetch<Student, StudentLab> studentLabFetch = from.fetch(Student_.studentLabs, JoinType.LEFT);
studentLabFetch.fetch(StudentLab_.discipline, JoinType.LEFT);
List<Student> students = getEntityManager().createQuery(query).getResultList();
log.trace("findAllWithLabsJpaCriteria: students={}", students);
return students;
}
}
我面临的错误是:
1 - 超级方法(“无法解析方法'超级(java.lang.Class)'”)
2 - 每个getEntityManager函数(“无法解析方法'getEntityManager'”)
3 - “Student_”和“StudentLab_”(“无法解析符号”)
在他的项目中,相同的代码,虽然有类名修改,但有效。是与.iml或pom.xml文件有关吗?
以下是导入的库:
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.jpa.HibernateEntityManager;
import org.hibernate.jpa.internal.EntityManagerFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.*;
import java.util.List;
import ro.ubb.books.core.model.*;
import ro.ubb.books.core.repository.*;
答案 0 :(得分:0)
如果您还复制了pom.xml,请尝试运行以下命令。
mvn clean package
注意:必须安装mvn(maven)。