Java 从关联表中选择时无法解决属性错误
我正在尝试从一个关联表中进行选择(从具有多对多关系的2个表中)。以下是我的实体: 问题:Java 从关联表中选择时无法解决属性错误,java,mysql,hibernate,hql,Java,Mysql,Hibernate,Hql,我正在尝试从一个关联表中进行选择(从具有多对多关系的2个表中)。以下是我的实体: 问题: @Entity @Table(name = "question") public class Question { @Id private int id; @Column(name = "question_text") private String text; @Column(name = "chapter_id") private int chapterId; @ManyToMany(mapp
@Entity
@Table(name = "question")
public class Question {
@Id
private int id;
@Column(name = "question_text")
private String text;
@Column(name = "chapter_id")
private int chapterId;
@ManyToMany(mappedBy = "questions")
@Transient
public Set<Assessment> assessments = new HashSet<Assessment>();
public Question(int id, String text, int chapterId) {
this.id = id;
this.text = text;
this.chapterId =chapterId;
}
//getters setters
但是,我收到以下例外情况:
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: assessments of: models.Question [select q from models.Question q join q.assessments a where a.id = :id]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:713)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
at dao.AssessmentQuestionDAO.getQuestionsOfAssessment(AssessmentQuestionDAO.java:60)
at webapp.Main.main(Main.java:156)
我在这里读了很多关于如何使用多对多以及如何解决这个问题的书,但到目前为止还没有什么能解决这个问题。看起来我错过了一些很小的东西,可能是我看不见的。你知道如何解决这个问题吗?
@Transient
是错误的,因此去掉它会给我一个错误,比如线程main org.hibernate.AnnotationException中的异常:使用@OneToMany或@ManyToMany针对未映射的类:models.Question.Assessment[models.Assessment]
。我以前试图解决这个问题,并得到了添加@Transient
的建议。这正是我在创建类之间的映射时遵循的教程。现在再次检查所有内容,仍然没有成功,然后删除不必要的注释(@Transient
和@ElementCollection
)。然后检查你的hibernate.cfg.xml
,确保添加Assessment
作为映射类@Transient
是错误的,因此删除它会给我一个错误,如线程main org.hibernate.AnnotationException中的异常:使用@OneToMany或@ManyToMany针对未映射类:models.Question.Assessment[models.Assessment]
。我以前试图解决这个问题,并得到了添加@Transient
的建议。这正是我创建类之间映射时遵循的教程。现在再次检查所有内容,仍然没有成功,然后删除不必要的注释(@Transient
和@ElementCollection
)。然后检查hibernate.cfg.xml
,确保将评估添加为映射类
@Transactional
public List<Question> getQuestionsOfAssessment(int id) {
List<Question> list = null;
Configuration con = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Question.class);
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
try{
TypedQuery<Question> query=sf.openSession().createQuery("select q from Question q join q.assessments a where a.id = :id");
query.setParameter("id",id);
list = query.getResultList();
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
return list;
}
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: assessments of: models.Question [select q from models.Question q join q.assessments a where a.id = :id]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:713)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
at dao.AssessmentQuestionDAO.getQuestionsOfAssessment(AssessmentQuestionDAO.java:60)
at webapp.Main.main(Main.java:156)