0

I'm trying to make an update query in HQL, but in doesn't work, I'm using MySQL5Dialect. In Patient class all variables are annotated just like column's names in database. 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
2
  • You're probably missing a space between disease= :disease, and condition= :condition. Commented Feb 12, 2016 at 23:16
  • I cut query since "condition" till the end, and everything was fine, when I added just only condition=:condition the problem appears again Commented Feb 12, 2016 at 23:30

1 Answer 1

1

The word condition is a keyword in MySQL, probably that's the reason of your error. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.