JPA+Hibernate+Mysql查询语言问题
Posted
技术标签:
【中文标题】JPA+Hibernate+Mysql查询语言问题【英文标题】:Query language problem with JPA +Hibernate+Mysql 【发布时间】:2019-09-03 22:28:22 【问题描述】:我已经开发了一个java独立应用程序(java+hibernate),现在我正在将它移植到java 8+springboot+jpa+hibernate+mysql框架。我在旧项目中使用了HQL,但是在新框架中SQL语句似乎存在一些问题,例如我必须将“From p where id=(:parameter)”更改为“Select * from p where id= :parameter" 去掉括号。
豆
@Entity
public class Place extends Groupunit_Defined implements Interface_Entity
......
@ManyToOne(targetEntity=Place_Definer.class)
@JoinColumn(name="Groupunit_Definer",nullable=false)
private Place_Definer Groupunit_Definer;//
.....
@Entity
public class Place_Definer extends Groupunit_Definer
......
@Column(nullable=false,length=150)
private String Classification;
…….
@SuppressWarnings("未选中") 公共 ArrayList Simply_get_thing_list ( 字符串查询字符串, ArrayList Parameter_List, 类 Thing_Class ) 查询 q=Entity_Manager.createNativeQuery(Query_String,Thing_Class); ……。 返回事物列表;
SQL 语句
"SELECT * FROM Place p WHERE p.Groupunit_Definer.Classification='Jurisdiction'"
休眠配置
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
错误信息
:: Spring Boot :: (v1.5.21.RELEASE)
2019-09-04 05:28:11.851 INFO 2508 --- [ main] c.s.mirrorworld.mirrorworldApplication : Starting mirrorworldApplication on LAPTOP-FAS0SHLM with PID 2508 (C:\Users\yueho\eclipse-workspace\mirrorworld\target\classes started by yueho in C:\Users\yueho\eclipse-workspace\mirrorworld)
2019-09-04 05:28:11.859 INFO 2508 --- [ main] c.s.mirrorworld.mirrorworldApplication : No active profile set, falling back to default profiles: default
2019-09-04 05:28:12.066 INFO 2508 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f217633: startup date [Wed Sep 04 05:28:12 CST 2019]; root of context hierarchy
2019-09-04 05:28:15.937 INFO 2508 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2019-09-04 05:28:15.994 INFO 2508 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-09-04 05:28:16.257 INFO 2508 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core 5.0.12.Final
2019-09-04 05:28:16.260 INFO 2508 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-09-04 05:28:16.263 INFO 2508 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2019-09-04 05:28:16.559 INFO 2508 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
2019-09-04 05:28:17.740 INFO 2508 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-09-04 05:28:19.002 WARN 2508 --- [ main] o.h.b.i.SessionFactoryBuilderImpl : Unrecognized hbm2ddl_auto value : update . Supported values include create, create-drop, update, and validate. Ignoring
2019-09-04 05:28:20.031 INFO 2508 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-04 05:28:24.577 INFO 2508 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-09-04 05:28:24.607 INFO 2508 --- [ main] c.s.mirrorworld.mirrorworldApplication : Started mirrorworldApplication in 13.439 seconds (JVM running for 24.733)
2019-09-04 05:28:33.326 INFO 2508 --- [WT-EventQueue-0] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2019-09-04 05:28:33.844 WARN 2508 --- [WT-EventQueue-0] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22
2019-09-04 05:28:33.845 ERROR 2508 --- [WT-EventQueue-0] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'p.Groupunit_Definer.Classification' in 'where clause'
Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at ……
org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:375)
at ……
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at …...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'p.Groupunit_Definer.Classification' in 'where clause'
【问题讨论】:
那里真的需要 targetEntity=Place_Definer.class 吗?如果 GROUPUNIT_DEFINER 列在 PLACE 表中,我通常不会明确给出它,它将使用当前表 (PLACE) 中的列。并且不是@Table(name="PLACE")...或类中缺少类似的东西(不确定是否需要或Spring可以从类名中检测到表名)? 您混淆了 JPQL 和 SQL。 SQL 使用表名和列名,并且有select *
。 JPAL 使用实体名称及其属性,并且没有 select *
。
谢谢JB Nizet,我把句子改成了“SELECT p.* FROM Place p WHERE p.Groupunit_Definer.Classification='Jurisdiction'”,但是没有用。
关于EntityManager的createNativeQuery方法,不知SQL语句是否可以是SQL、JPQL、HQL等任何一种SQL语言?
【参考方案1】:
问题已通过将“createNativeQuery”改为“createQuery”解决,谢谢
【讨论】:
以上是关于JPA+Hibernate+Mysql查询语言问题的主要内容,如果未能解决你的问题,请参考以下文章
使用 spring boot JPA hibernate 存储库检查用户名和密码的 Mysql 查询不起作用。我该怎么办?
如何使用 JPA/Hibernate 注释将 MySQL char(n) 列映射到实例变量?
Spring webflow + Jpa + Hibernate运行时无响应问题处理