[英]JPA Exception: org.hibernate.exception.SQLGrammarException
I'm developing a Spring MVC application with MySQL DB.我正在使用 MySQL DB 开发 Spring MVC 应用程序。 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.我的问题是,当 SQL 语句被释放(使用 hibernate JPA)时, org.hibernate.exception.SQLGrammarException 会随此信息一起抛出。
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
¿Why?为什么?
---EDIT--- - -编辑 - -
My mysql configuration is我的mysql配置是
@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 SQL INSERT 测验表是
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 、 spring boot 、 spring data 、 JPA 和 Hibernate,请尝试使用以下属性:
spring.jpa.properties.hibernate.globally_quoted_identifiers=true spring.jpa.properties.hibernate.globally_quoted_identifiers=true
If you are using hibernate only :如果您只使用休眠:
hibernate.globally_quoted_identifiers=true hibernate.globally_quoted_identifiers=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.