Limmy
Limmy

Reputation: 746

Hibernate can't save entity without renaming

I have very strange problem and I can't understand it's reason.

I tried to save simple Object (Group.class) with only 1 field (@id groupName), getter and setter. When I try to run my program, I have an exception:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update at

...

Caused by: java.sql.BatchUpdateException: 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 'Group (groupName) values ('Test')' at line 1

But if I specify the name of entity (different of Group) all works fine. Could you please explain me what I've done wrong?

Code:

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

        <mapping class="com.globallogic.dto.Group"></mapping>


    </session-factory>

</hibernate-configuration>

import javax.persistence.*;

Group.class

@Entity //this doesn't work
//@Entity(name = "SomeName") // this works good
public class Group {

    @Id
    private String groupName;

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
    public String getGroupName() {
        return groupName;

    }

}

DataSender.class

public class DataSender {
    private SessionFactory sessionFactory;
    public DataSender() {
        sessionFactory = new Configuration().configure()
                .buildSessionFactory();
    }
    public void sendData(Object... objects) {
        Session session = sessionFactory.openSession();
        if (session.isConnected()) {

            System.out.println("ALL IS GOOD");
            session.beginTransaction();

            for (Object user: objects) {
                session.persist(user);
            }
            System.out.println();
            session.getTransaction().commit();
            session.close();
            System.out.println("Connection is closed");
        }

        sessionFactory.close();
    }

}

Main.class

public class Main {

    public static void main(String[] args) {


        DataSender ds = new DataSender();

        Group group = new Group();
        group.setGroupName("Test");
        ds.sendData(group);

    }
}

Upvotes: 1

Views: 92

Answers (1)

Jord&#227;o
Jord&#227;o

Reputation: 56517

Group is an SQL keyword, you'll need to quote your table name:

@Entity
@Table(name = "`Group`")
public class Group {
  ....

Upvotes: 1

Related Questions