#java #hibernate #entity
Вопрос:
Я пытаюсь создать таблицу и получаю сообщение об ошибке, я не знаю, почему. Я думал, что это из-за поплавка, но в моей таблице полей есть поплавок, и он генерируется правильно.
Я пытался изменить диалект или создать свойство удаления. Но мне это не помогает. Так что я действительно не знаю, что делать.
Кроме того, я думал, что ранг столбца является причиной ошибки, но я не знаю, почему
Ошибка:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table index (id integer not null, lemma_id integer not null, page_id integer not null, rank float not null, primary key (id)) engine=MyISAM" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:439)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:423)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:314)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:166)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:156)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:318)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
at loader.Loader.main(Loader.java:22)
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index (id integer not null, lemma_id integer not null, page_id integer not null,' at line 1
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.StatementImpl.executeInternal(StatementImpl.java:762)
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:646)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
... 11 more
Перейти в режим гибернации cfg
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/search_engine?useSSL=false</property>
<property name="connection.username">root</property>
<property name="connection.password">roottoor</property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping class="loader.scanner.Website"></mapping>
<mapping class="loader.entity.Field"></mapping>
<mapping class="loader.entity.Lemma"></mapping>
<mapping class="loader.entity.Index"></mapping>
</session-factory>
</hibernate-configuration>
Индексируемая сущность
package loader.entity;
import javax.persistence.*;
@Entity
@Table(
name = "index"
)
public class Index {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(
name = "page_id",
nullable = false
)
private int pageId;
@Column(
name = "lemma_id",
nullable = false
)
private int lemmaId;
@Column(
nullable = false
)
private float rank;
public Index(int pageId, int lemmaId, float rank) {
this.pageId = pageId;
this.lemmaId = lemmaId;
this.rank = rank;
}
public Index(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPageId() {
return pageId;
}
public void setPageId(int pageId) {
this.pageId = pageId;
}
public int getLemmaId() {
return lemmaId;
}
public void setLemmaId(int lemmaId) {
this.lemmaId = lemmaId;
}
public float getRank() {
return rank;
}
public void setRank(float rank) {
this.rank = rank;
}
@Override
public String toString() {
return "Index{"
"id=" id
", pageId=" pageId
", lemmaId=" lemmaId
", rank=" rank
'}';
}
}
Ответ №1:
Индекс и ранг являются зарезервированными ключевыми словами в SQL. Попробуйте сменить его на что-нибудь другое.
Но если вы хотите использовать только индекс, используйте ниже.
@Table(name = ""index"")
Комментарии:
1. К сожалению, я попытался изменить имя таблицы, имена столбцов, но это не помогло
2. Даже ранг-это ключевое слово.
3. Большое вам спасибо, не думал о смене обоих имен
4. Да, это так, спасибо. Я изменил название таблицы и ранг столбца, как вы сказали, так что это работает!