java.sql.SQLSyntaxErrorException: Unknown column 't1_0.division' in 'field list' using Hibernate

huangapple go评论68阅读模式
英文:

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_namedivdiv_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(&quot;from Team where id=1234&quot;).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 &quot;main&quot; 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\] &lt;5 internal lines&gt;
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\] &lt;21 internal lines&gt;
Caused by: java.sql.SQLSyntaxErrorException: Unknown column &#39;t1_0.division&#39; in &#39;field list&#39;
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 = &quot;Team&quot;)
@Table(name = &quot;team&quot;, schema = &quot;test&quot;)
public class Team implements Serializable {
@Serial
private static final long serialVersionUID = -8929555475128771601L;
@Id
@Column(name = &quot;team_id&quot;, nullable = false)
private int id;
@Column(name = &quot;team_name&quot;, nullable = false, length = 100)
private String name;
@Column(name = &quot;team_abbr&quot;, nullable = false, length = 50)
private String abbreviation;
@Column(name = &quot;division&quot;, length = 46)
private String divisionName;
@Column(name = &quot;score&quot;, length = 30)
private String score;
}

I have tried nearly everything:

  • I renamed the column in division_name, div and div_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:

&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/bot?serverTimezone=UTC&lt;/property&gt;
&lt;property name=&quot;hibernate.default_schema&quot;&gt;test&lt;/property&gt;

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.

&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/test?serverTimezone=UTC&lt;/property&gt;
&lt;property name=&quot;hibernate.default_schema&quot;&gt;test&lt;/property&gt;

huangapple
  • 本文由 发表于 2023年2月8日 19:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定