A
A
Andrey Kovalchuk2016-06-08 16:30:29
MySQL
Andrey Kovalchuk, 2016-06-08 16:30:29

How to update a record with JpaRepository\Hibernate?

Good day. An error pops up (error code below), when the component is running (immediately below). Without trying to save Room, everything flies. What am I doing wrong? The driver is added, the path to it is specified in the settings, the settings are tightened.
Component where work with repositories is carried out

package com.kovalchuk.server.config.arduino;

 *IMPORT*

@Lazy
@Component
@EnableScheduling
@ComponentScan("com.kovalchuk.server")
@EnableJpaRepositories("com.kovalchuk.server.repository")
public class ArduinoEventsListener implements SerialPortEventListener {

    private SerialPort serialPort = ArduinoConfig.getSerialPort();
    private RoomService roomService = ArduinoConfig.getRoomService();
    private CommitService commitService = ArduinoConfig.getCommitService();
    @Override
    public void serialEvent(SerialPortEvent event) {
        try {
            String buffer;
            String[] data;
            do {
                buffer = serialPort.readString(13,60000);
                data = buffer.replaceAll("\r|\n", "").split(":");

            } while (data[1].equals("0")||data[2].equals("0")||data[3].equals("0"));


            Commit commit = new Commit();

            commit.setTemperature(Double.parseDouble(data[2]));
            commit.setWet(data[3]);
            commit.setDate(new Date());

            try {
                Room room = roomService.findByIndex(data[1]);
                commit.setRoom(room);
                room.setStatus(commit);
                commitService.save(commit);
                roomService.save(room);
            }
            catch(NullPointerException e){
                serialPort.closePort();
            }




        } catch (SerialPortException e) {
            e.printStackTrace();
        } catch (SerialPortTimeoutException e) {
            e.printStackTrace();
        }
    }


}

commit.java
package com.kovalchuk.server.entity;


*IMPORT*

/**
 * Объект, содержащий в себе все статистические данные полученные от Arduino.
 */
@Entity
@Table(name = "commit")
public class Commit implements Serializable {

    @Id
    @Column(name ="Id")
    @GeneratedValue (generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private Long id;

    @Column(name = "Temperature")
    private Double temperature;

    @Column(name = "Date", nullable = false)
    private Date date;

    @Column(name = "Wet")
    private String wet;

    @ManyToOne
    @JoinColumn(name = "Room")
    private Room room;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

    public Commit(Double temperature, Date date, String wet, Room room) {

        this.temperature = temperature;
        this.date = date;
        this.wet = wet;
        this.room = room;
    }

    public Commit() {
    }


    public Commit(Double temperature, Date date, String wet) {
        this.temperature = temperature;
        this.date = date;
        this.wet = wet;
    }

    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getWet() {
        return wet;
    }

    public void setWet(String wet) {
        this.wet = wet;
    }
}

Room.java
package com.kovalchuk.server.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity
@Table (name= "room")
public class Room {

    @Id
    @Column(name ="idRoom")
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private Long id;

    @Column(name="Index")
    private String index;

    @Column(name = "Type")
    private String type;

    @ManyToOne
    @JoinColumn(name = "Status")
    private Commit status;

    public Room(String index) {
        this.index = index;
    }


    public Room(String index, String type, Commit status) {
        this.index = index;
        this.type = type;
        this.status = status;
    }

    public Room(String index, String type) {
        this.index = index;
        this.type = type;
    }

    public Room() {
    }

    public Commit getStatus() {
        return status;
    }

    public void setStatus(Commit status) {
        this.status = status;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

Error code
Hibernate: select commit0_.Id as Id1_1_0_, commit0_.Date as Date2_1_0_, commit0_.Room as Room5_1_0_, commit0_.Temperature as Temperat3_1_0_, commit0_.Wet as Wet4_1_0_, room1_.idRoom as idRoom1_2_1_, room1_.Index as Index2_2_1_, room1_.Status as Status4_2_1_, room1_.Type as Type3_2_1_, commit2_.Id as Id1_1_2_, commit2_.Date as Date2_1_2_, commit2_.Room as Room5_1_2_, commit2_.Temperature as Temperat3_1_2_, commit2_.Wet as Wet4_1_2_ from commit commit0_ left outer join room room1_ on commit0_.Room=room1_.idRoom left outer join commit commit2_ on room1_.Status=commit2_.Id where commit0_.Id=?
Hibernate: update room set Index=?, Status=?, Type=? where idRoom=?
июн 08, 2016 11:19:47 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1064, SQLState: 42000
июн 08, 2016 11:19:47 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 'Index='1450', Status=187, Type='Lab' where idRoom=1' at line 1
июн 08, 2016 11:19:47 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
июн 08, 2016 11:19:47 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 1064, SQLState: 42000
июн 08, 2016 11:19:47 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: 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 'Index='1450', Status=187, Type='Lab' where idRoom=1' at line 1
Exception in thread "EventThread COM3" org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:238)
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
  at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179.......
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
  at com.sun.proxy.$Proxy47.save(Unknown Source)
  at com.kovalchuk.server.services.impl.RoomServiceImpl.save(RoomServiceImpl.java:29)
  at com.kovalchuk.server.config.arduino.ArduinoEventsListener.serialEvent(ArduinoEventsListener.java:52)
  at jssc.SerialPort$EventThread.run(SerialPort.java:1112)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
  at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
  at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
  ......
  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
  at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
  at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
  at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
  at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
  at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
  at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
  ... 17 more
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 'Index='1450', Status=187, Type='Lab' where idRoom=1' at line 1
  ...
  ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Severus256, 2016-06-08
@mrkovalchuk

Check database queries, entities, whether your annotations like @ManyToOne are correct, and so on. Everything is in the log.

A
Alexander Kosarev, 2016-06-09
@jaxtr

index is a reserved word in MySQL
Choose column names that do not conflict with reserved words.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question