使用 Hibernate Spatial 和 MySQL 5.6 获取 MySQLSyntaxErrorException

Getting MySQLSyntaxErrorException with Hibernate Spatial and MySQL 5.6

我正在使用以下堆栈: Hibernate 5.2.10.FINAL(hibernate-spatial,hibernate-java8) MySQL 5.6.38(也在最新的 5.7 上进行了测试,结果相同) querydsl-apt, querydsl-jpa, querydsl-sql-空间
都是基于Spring Boot.

我试图在我的数据库中保留这个简单的 class:

@Entity
public class ParkingPlace extends AbstractEntity {

@Column(name = "location", nullable = false, columnDefinition = "Point")
private Point geoLoc;

private String address;
private String city;
private String name;
private String desc;

private int capacity;
private int freePlacesLeft;

@ManyToOne
@JoinColumn(name = "owner_id")
private Owner owner;
}

抽象实体是:

@MappedSuperclass
public abstract class AbstractEntity implements Serializable, Cloneable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Version
private int version;
}

我也有这个 Repository 接口:

@NoRepositoryBean
public interface JpaQueryDslPredicateRepository<T, ID extends Serializable>
    extends QueryDslPredicateExecutor<T>, JpaRepository<T, ID> {
@Override
List<T> findAll(OrderSpecifier<?>... orders);

@Override
List<T> findAll(Predicate predicate);

@Override
List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);

@Override
List<T> findAll(Predicate predicate, Sort sort);
}

还有我的 ParkingPlaceRepository:

public interface ParkingPlaceRepository
    extends JpaQueryDslPredicateRepository<ParkingPlace, Long> {}

最后一个重要节拍是我正在使用的属性:

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
spring.jpa.show-sql=true
spring.datasource.platform=mysql
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type=TRACE

我也尝试过以下方言:MySQL5SpatialDialect,MySQL56InnoDBSpatialDialect(已弃用但用于 project 我基于),MySQLSpatialDialect .

我正在尝试使用此函数用初始数据集填充 table:

    private static void addPlaceToRepo(ParkingPlaceRepository repo, GeometryFactory factory, double lat, double lon, String city, String address, String name, int capacity) {
    ParkingPlace parkingPlace = new ParkingPlace();

    parkingPlace.setGeoLoc(factory.createPoint(new Coordinate(lat, lon)));
    parkingPlace.setCity(city);
    parkingPlace.setAddress(address);
    parkingPlace.setName(name);
    parkingPlace.setCapacity(capacity);

    repo.save(parkingPlace);
}

遗憾的是,尽管我尽了最大的努力,我仍然遇到这样的错误:

Hibernate: alter table order drop foreign key FKbb5wakyppwqmfuhp53p3jvs5u
gru 19, 2017 9:51:15 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'order drop foreign key FKbb5wakyppwqmfuhp53p3jvs5u' at line 1

这里声明 table 实际上并没有创建,我在 MySQL Workbench 的调试过程中看到它确实不是:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'ppclone.parking_place' doesn't exist

但是为什么呢?它是 create-drop,应该可以工作。

可以看到完整的日志here。我很高兴能得到你的帮助!

您必须按如下方式注释您的实体 class :

    @Entity
    @Table(name = "parking_place")
    public class ParkingPlace extends AbstractEntity {
    ...
      @Column(name = "name")
      private String name;
    ...
    }

@Entity

@Entity 将此 class 标记为实体 bean,因此它必须具有至少在受保护范围内可见的无参数构造函数。

@Table

@Table 批注允许您指定 table 的详细信息,这些详细信息将用于将实体保存在数据库中。

@Column

@Column 注释用于指定字段或 属性 将映射到的列的详细信息。

检查此以获取更多详细信息click here

感谢 ,我找到了解决方案。 回答desc是MySQL中的保留关键字,更改字段名称或提供列名称除'desc'以外的其他内容!