简体   繁体   中英

JPA Exception: org.hibernate.exception.SQLGrammarException

I'm developing a Spring MVC application with MySQL DB. Two of the tables in the database have a one to many relationship.

One of this tables is Quiz

@Entity
@Table(name="Quiz", schema = "QuizOwo")
public class QuizEntity
{
   @Id
   @GeneratedValue(strategy= GenerationType.IDENTITY)
   @Column(name = "idQuiz")
    private int idQuiz;

   @Column(name = "nameQuiz")
    private String nameQuiz;

   @Column(name = "descriptionQuiz")
    private String descriptionQuiz;

   @ManyToOne(optional=false)
   @JoinColumn(name = "idCategory")
    private CategoryQuizEntity categoryQuizEntity;

   @Column(name = "idPortal")
    private int idPortal;

The other table is

@Entity
@Table(name="CategoryQuiz", schema = "QuizOwo")
public class CategoryQuizEntity
{
   @Id
   @GeneratedValue(strategy= GenerationType.IDENTITY)
   @Column(name = "idCategoryQuiz")
    private int idCategoryQuiz;

   @Column(name = "nameCategory")
    private String nameCategory;

   @OneToMany(mappedBy="categoryQuizEntity", targetEntity=QuizEntity.class, fetch=FetchType.LAZY)
   @LazyCollection(LazyCollectionOption.FALSE)
     private List<QuizEntity> quizEntities;

My problem is that when a SQL statement is released (using hibernate JPA) the org.hibernate.exception.SQLGrammarException is thrown with this information.

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

¿Why?

---EDIT---

My mysql configuration is

@Configuration
@EnableTransactionManagement
public class PersistenceConfig
{
    /** Bean para obtener el dataSource */
    @Bean(name = "dataSource")
    public DataSource getDataSource(){
       DataSource dataSource = createDataSource();
       return dataSource;
     }

    /** Generador del datasource */
    private DriverManagerDataSource createDataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(ConfigurationManager.getProperty(ConfigurationManager.DATABASE_DIVERCLASSNAME));
        driverManagerDataSource.setUrl(ConfigurationManager.getProperty(ConfigurationManager.DATABASE_URL));
        driverManagerDataSource.setUsername(ConfigurationManager.getProperty(ConfigurationManager.DATABASE_USERNAME));
        driverManagerDataSource.setPassword(ConfigurationManager.getProperty(ConfigurationManager.DATABASE_PASSWORD));
        driverManagerDataSource.setConnectionProperties(databaseProperties());
        return driverManagerDataSource;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }


    /** Generamos un entityManagerFactory con lo paquetes donde tiene que escanear para buscar a las entidades, el datasource
 * las propiedades de hibernate... */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException, PropertyVetoException {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(createDataSource());
        emf.setPackagesToScan(new String[] { "com.owo.quiz.models" });
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        emf.setJpaVendorAdapter(vendorAdapter);
        emf.setJpaProperties(hibernateProperties());
        return emf;
    }

    /** Propiedades para hibernate/JPA */
    Properties hibernateProperties() {
        return new Properties() {
            {
                setProperty("hibernate.dialect", ConfigurationManager.getProperty(ConfigurationManager.HIBERNATE_DIALECT));
                setProperty("hibernate.globally_quoted_identifiers", "true");
            }
        };
    }

    /** Propiedades para la conexión con la base de datos */
    Properties databaseProperties() {
        return new Properties() {
            {
                setProperty("characterEncoding", "iso8859-1");
            }
        };
    }
}

=== EDIT ====

The SQL INSERT Quiz Table is

CREATE TABLE Quiz ( idQuiz int(11) NOT NULL AUTO_INCREMENT, nameQuiz varchar(45) COLLATE latin1_spanish_ci DEFAULT NULL, descriptionQuiz text COLLATE latin1_spanish_ci, idCategory int(11) DEFAULT NULL, idPortal int(11) DEFAULT NULL, PRIMARY KEY (idQuiz), KEY fk_Quiz_1_idx (idCategory), CONSTRAINT fk_Quiz_1 FOREIGN KEY (idCategory) REFERENCES CategoryQuiz (idCategoryQuiz) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci;

我重置了该用户的所有 MySQL 权限并且它工作正常。

尝试将 MySQL 方言设置为连接属性的一部分。

hibernate.dialect=org.hibernate.dialect.MySQLDialect

If you are using spring , spring boot , spring data , JPA and Hibernate, try using below property:

spring.jpa.properties.hibernate.globally_quoted_identifiers=true

If you are using hibernate only :

hibernate.globally_quoted_identifiers=true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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