简体   繁体   English

Hibernate中的MySQL语法-怎么了?

[英]MySQL syntax in Hibernate - what's wrong?

I'm trying to make an update query in HQL, but in doesn't work, I'm using MySQL5Dialect. 我正在尝试在HQL中进行更新查询,但是在不起作用的情况下,我正在使用MySQL5Dialect。 In Patient class all variables are annotated just like column's names in database. 在Patient类中,所有变量都被注释,就像数据库中列的名称一样。 Here is a code: 这是一个代码:

Controller:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("clinic");
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();

    Patient patient = new Patient();

    patient.setFirstName(infonameField.getText());
    patient.setLastName(infolastNameField.getText());
    patient.setGender(infogenderField.getText());
    patient.setAge(infoageField.getText());
    patient.setPhonenumber(infophonenumberField.getText());
    patient.setAddress(infoadressField.getText());
    patient.setDisease(infodiseaseField.getText());
    patient.setCondition(infoconditionField.getText());
    patient.setRoomtype(infoRoomType.getText());
    patient.setRoomNumber(infoRoomNumber.getText());
    patient.setDateregistration(infodataField.getText());
    patient.setIdpatient(infoPIDField.getText());

    Query query = entityManager.createQuery("UPDATE Patient set name = :name, lastname= :lastname,"
            + "gender= :gender, age= :age, phonenumber= :phonenumber, address= :address, disease= :disease,"
            + "condition= :condition, roomtype= :roomtype, roomnumber= :roomnumber, "
            + "dateregistration= :dateregistration WHERE idpatient= :idpatient");

    query.setParameter("name", patient.getFirstName());
    query.setParameter("lastname", patient.getLastName());
    query.setParameter("gender", patient.getGender());
    query.setParameter("age", patient.getAge());
    query.setParameter("phonenumber", patient.getPhonenumber());
    query.setParameter("address", patient.getAddress());
    query.setParameter("disease", patient.getDisease());
    query.setParameter("condition", patient.getCondition());
    query.setParameter("roomtype", patient.getRoomtype());
    query.setParameter("roomnumber", patient.getRoomNumber());
    query.setParameter("dateregistration", patient.getDateregistration());
    query.setParameter("idpatient", patient.getIdpatient());
    query.executeUpdate();

    entityManager.getTransaction().commit();

Error: 错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'condition='ewqewqeq', roomtype='standard', roomnumber='101', dateregistration='2' at line 1

The word condition is a keyword in MySQL, probably that's the reason of your error. condition一词是MySQL中的一个关键字 ,可能就是您错误的原因。 Try modifying it or replace it by another name. 尝试对其进行修改或将其替换为其他名称。

EDIT 编辑

After reading this MySQL forum post, you can use it as long as you escape it, as mentioned here . 看完这个 MySQL的论坛上发帖,你可以只要你逃避它使用它,因为提到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM