我有访问结果的标准
第一个 Hibernate Dao 是
AnswerText answersText = questionManager.getAnswerTextByAnswerIdAndLanguageId(answers.getAnswerId(), 1L);
@Override
public AnswerText getAnswerTextByAnswerIdAndLanguageId(Number answerId,Number languageId) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(AnswerText.class);
criteria.add(Restrictions.eq("answer.answerId", answerId));
criteria.add(Restrictions.eq("languageId", languageId));
List<AnswerText> results = criteria.list();
return (results !=null && !results.isEmpty()? results.get(0): null);
}
Answers.java
@Entity
@Table(name = "ANSWERS")
@Cacheable
@JsonIgnoreProperties(ignoreUnknown = true)
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ANSWER_ID")
private Long answerId;
@Column(name = "QUESTION_ID")
private Long questionId;
@Column(name = "DATE_CREATED")
private Timestamp dateCreated;
@Column(name = "CREATED_BY_ID")
private Long creatorId;
@Column(name = "DATE_MODIFIED")
private Timestamp dateModified;
@Column(name = "MODIFIED_BY_ID")
private Long modifierId;
@OneToMany(fetch = FetchType.EAGER,mappedBy = "answer" )
private Set<AnswerText> answerText = new HashSet<AnswerText>();
//getters and setters
AnswerText.java
@Entity
@Table(name = "ANSWERTEXT")
@Cacheable
@JsonIgnoreProperties(ignoreUnknown = true)
public class AnswerText {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ANSWER_TEXT_ID")
private Long answerTextId;
@ManyToOne
@JoinColumn(name="answerId", insertable=false, updatable=false,
nullable=false)
private Answer answer;
@Column(name = "ANSWER_TEXT")
private String answerText;
@Column(name = "LANGUAGE_ID")
private Long languageId;
//getters and setters
当我访问获取结果集时,它显示以下错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'answertext2_.answerId' in 'field list'
然后我改成下面的
AnswerText.java
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ANSWER_ID", nullable = false)
private Answer answer;
答案.java
@OneToMany(fetch = FetchType.EAGER,mappedBy = "answer" )
@Fetch(FetchMode.JOIN)
private Set<AnswerText> answerText = new HashSet<AnswerText>();
这不会产生错误,但在调用时会两次获取结果
第二个 HibernateDao 调用是
List<Answer> answerList = questionManager.getAnswersByQuestionId(Long.parseLong("2"));
System.out.println("answerList :"+answerList1.size());
@Override
public ArrayList<Answer> getAnswersByQuestionId(Number questionId) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Answer.class);
criteria.add(Restrictions.eq("questionId", questionId));
ArrayList<Answer> answerList = (ArrayList) criteria.list();
return answerList;
}
请任何人指出我这里出了什么问题。请帮助我。
最佳答案
您已经正确更改了 JoinColumn-name。
我认为您的多个结果必然是:
FetchType.EAGER和选择
FetchMode JOIN As indicated you’ll have to worry about duplicated results. On the other hand JOIN creates the least amount of queries. In a high latency environment a single JOIN could be considerable faster then multiple SELECTS. Keep in mind that joining too much data could put a strain on the database. from http://www.solidsyntax.be/2013/10/17/fetching-collections-hibernate/
使用FetchMode.SELECT,您将得到您想要的结果。
关于mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column in 'field list' using Criteria and mapping annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534601/