Java 如果出现';试图在数据库中保存对象时由hibernate生成的?
我正在尝试使用hibernate中的session.save()方法保存对象。到目前为止,其他类的一切都运行得很好,但现在我被这个SQL语法错误所困扰,到目前为止,我还没有发现任何有用的东西。以下是控制台结果:Java 如果出现';试图在数据库中保存对象时由hibernate生成的?,java,mysql,hibernate,Java,Mysql,Hibernate,我正在尝试使用hibernate中的session.save()方法保存对象。到目前为止,其他类的一切都运行得很好,但现在我被这个SQL语法错误所困扰,到目前为止,我还没有发现任何有用的东西。以下是控制台结果: Error update: could not execute statement May 23, 2019 5:47:26 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: You have
Error update: could not execute statement
May 23, 2019 5:47:26 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option (is_answer, question_id, option_text) values (0, 97, 'asdfaoins')' at line 1
下面是我的QuestionDAO类中的方法:
@Transactional
public void addQuestionOption(String text, int questionId, boolean isAnswer) {
QuestionOption newOption = new QuestionOption();
newOption.setText(text);
newOption.setQuestionId(questionId);
newOption.setIsAnswer(isAnswer);
Configuration con = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(QuestionOption.class);
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
try {
Transaction tx = session.beginTransaction();
session.save(newOption);
tx.commit();
} catch (Exception e) {
System.out.println("Error update: " + e.getMessage());
session.getTransaction().rollback(); // get
} finally {
session.close();
}
以下是我的问题选项模型类:
@Entity
@Table(name = "option")
public class QuestionOption {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "option_text")
private String text;
@Column(name = "question_id")
private int questionId;
@Column(name = "is_answer")
private boolean isCorrectAnswer;
public QuestionOption(int id, String text, int questionId, boolean isAnswer) {
super();
this.id = id;
this.text = text;
this.questionId = questionId;
this.isCorrectAnswer = isAnswer;
}
public QuestionOption() {}
//getters & setters
我接到的电话就这么简单:
QuestionOptionDAO odao = new QuestionOptionDAO();
odao.addQuestionOption("asdfaoins", 97, false);
我提到,只要硬编码一些条目,我就可以毫无问题地插入工作台。我也尝试了
saveOrUpdate()
方法。另外,我对其他类使用了完全相同的方法,插入操作非常有效。知道我为什么会在这种情况下出现这个错误吗?这可能是因为选项在MySQL中是一个保留字
试着给你的桌子打电话,或者试试这里的建议
e、 g
也许您应该尝试记录Hibernate发送到DB的SQL语句
这是描述的
将问题粘贴到SQL客户机()中会更容易找到问题。或者将整个查询张贴在此处,以便我们可以查看。您可以添加您的表结构吗?
@Entity
@Table("\"Option\"")
public class QuestionOption {
...