英文:
java.sql.SQLSyntaxErrorException: Unknown column 't1_0.division' in 'field list' using Hibernate
问题
在执行查询时,我收到以下异常:
this.session.createQuery("from Team where id=1234").getSingleResult();
当我运行查询时,我会得到:
Hibernate: select t1_0.team_id,t1_0.team_abbr,t1_0.division,t1_0.team_name,t1_0.score from team t1_0 where t1_0.team_id=1234
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
Exception in thread "main" jakarta.persistence.PersistenceException: Converting org.hibernate.exception.SQLGrammarException to JPA PersistenceException : JDBC exception executing SQL [select t1_0.team_id,t1_0.team_abbr,t1_0.division,t1_0.team_name,t1_0.score from team t1_0 where t1_0.team_id=1234] <5 internal lines>
at de.zahrie.trues.truebot.PrimeData.init(PrimeData.java:47)
at de.zahrie.trues.truebot.PrimeData.init(PrimeData.java:47)
at de.zahrie.trues.truebot.PrimeData.getInstance(PrimeData.java:23)
at de.zahrie.trues.truebot.handler.LoadupManager.init(LoadupManager.java:16)
at de.zahrie.trues.truebot.Bot.main(Bot.java:9)
Caused by: org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL [select t1_0.team_id,t1_0.team_abbr,t1_0.division,t1_0.team_name,t1_0.score from team t1_0 where t1_0.team_id=1234] <21 internal lines>
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 't1_0.division' in 'field list'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:972)
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:217)
当我在MySQL Workbench中运行相同的查询时,我得到:
team_id | team_abbr | division | team_name | score |
---|---|---|---|---|
1234 | TEST | Division 0 | Testteam | 0:0 |
这是我的实体类:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Entity(name = "Team")
@Table(name = "team", schema = "test")
public class Team implements Serializable {
@Serial
private static final long serialVersionUID = -8929555475128771601L;
@Id
@Column(name = "team_id", nullable = false)
private int id;
@Column(name = "team_name", nullable = false, length = 100)
private String name;
@Column(name = "team_abbr", nullable = false, length = 50)
private String abbreviation;
@Column(name = "division", length = 46)
private String divisionName;
@Column(name = "score", length = 30)
private String score;
}
我尝试了几乎所有的方法:
- 将列重命名为
division_name
,div
和div_name
。 - 删除这个单独的列(之后它可以正常工作)。
- 尝试了
session.get(Team.class, 1234);
- 尝试了NamedQuery、NativeQuery和HQLQuery。
- 在我的
hibernate.cfg.xml
和实体类内部使用了模式定义。 - 将
division
提取到一个MappedClass中。
但是没有任何帮助。没有其他Stackoverflow上的帖子能够给我答案。问题出在哪里?
英文:
Im getting the following Exception while executing the query:
this.session.createQuery("from Team where id=1234").getSingleResult();
When I run the query I will get:
Hibernate: select t1_0.team_id,t1_0.team_abbr,t1_0.division,t1_0.team_name,t1_0.score from team t1_0 where t1_0.team_id=1234
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
Exception in thread "main" jakarta.persistence.PersistenceException: Converting org.hibernate.exception.SQLGrammarException to JPA PersistenceException : JDBC exception executing SQL \[select t1_0.team_id,t1_0.team_abbr,t1_0.division,t1_0.team_name,t1_0.score from team t1_0 where t1_0.team_id=1234\] <5 internal lines>
at de.zahrie.trues.truebot.PrimeData.init(PrimeData.java:47)
at de.zahrie.trues.truebot.PrimeData.init(PrimeData.java:47)
at de.zahrie.trues.truebot.PrimeData.getInstance(PrimeData.java:23)
at de.zahrie.trues.truebot.handler.LoadupManager.init(LoadupManager.java:16)
at de.zahrie.trues.truebot.Bot.main(Bot.java:9)
Caused by: org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL \[select t1_0.team_id,t1_0.team_abbr,t1_0.division,t1_0.team_name,t1_0.score from team t1_0 where t1_0.team_id=1234\] <21 internal lines>
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 't1_0.division' in 'field list'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:972)
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:217)
When I run the same query in the MySQL-Workbench I get:
team_id | team_abbr | division | team_name | score |
---|---|---|---|---|
1234 | TEST | Division 0 | Testteam | 0:0 |
Here is my Entity-Class:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Entity(name = "Team")
@Table(name = "team", schema = "test")
public class Team implements Serializable {
@Serial
private static final long serialVersionUID = -8929555475128771601L;
@Id
@Column(name = "team_id", nullable = false)
private int id;
@Column(name = "team_name", nullable = false, length = 100)
private String name;
@Column(name = "team_abbr", nullable = false, length = 50)
private String abbreviation;
@Column(name = "division", length = 46)
private String divisionName;
@Column(name = "score", length = 30)
private String score;
}
I have tried nearly everything:
- I renamed the column in
division_name
,div
anddiv_name
. - I have removed this single column (after that it works).
- I tried
session.get(Team.class, 1234);
- I tried NamedQuery, NativeQuery and HQLQuery.
- I used the Schema-Definition in my
hibernate.cfg.xml
and inside the Entity-Class. - I extracted the
division
into a MappedClass
but nothing helps. No other post on Stackoverflow could give me an answer. What is the issue?
I renamed the column in division_name, div and div_name.
I have removed this single column.
I tried session.get(Team.class, 1234);
I tried NamedQuery, NativeQuery and HQLQuery.
I used the Schema-Definition in my hibernate.cfg.xml and inside the Entity-Class.
I extracted the division into a MappedClass
答案1
得分: 0
我发现了问题:
我的hibernate.cfg看起来像这样:
<property name="connection.url">jdbc:mysql://localhost:3306/bot?serverTimezone=UTC</property>
<property name="hibernate.default_schema">test</property>
MySQL用户的名称是bot
,数据库也是如此。我以为URL中的参数是MySQL用户,而不是模式的名称。因此它在另一个模式中查找。像下面这样替换它就可以解决问题:
<property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
<property name="hibernate.default_schema">test</property>
英文:
I found the issue:
My hibernate.cfg looked like this:
<property name="connection.url">jdbc:mysql://localhost:3306/bot?serverTimezone=UTC</property>
<property name="hibernate.default_schema">test</property>
The MySQL-User is named bot
and the other database as well. I thought the argument inside the url is the MySQL user, not the name of the schema. Therefore it was looking inside an other schema. Replacing it like below solves the problem.
<property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
<property name="hibernate.default_schema">test</property>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论