com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表中的未知列'使用Criteria和映射注释

时间:2017-04-21 05:30:10

标签: mysql hibernate-mapping hibernate-criteria

我有通过

访问结果的标准

首先是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

当我访问fetch结果集时,它显示以下错误

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;

Answers.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;
}

请有人指出我这里出了什么问题。请帮助我。

1 个答案:

答案 0 :(得分:0)

您已正确更改JoinColumn-name。

我认为您的多重结果必然会:

FetchType.EAGER 以及

的选择
  

FetchMode JOIN 如上所示,您将不得不担心重复   结果。另一方面,JOIN创建的查询量最少。   在高延迟环境中,单个JOIN可能相当大   比多个SELECTS更快。请记住加入太多数据   可能会给数据库带来压力。   来自http://www.solidsyntax.be/2013/10/17/fetching-collections-hibernate/

使用 FetchMode.SELECT ,您可以获得想要的结果。