首页 > 解决方案 > 带有附加参数的 JPA 关系 ManyToMany

问题描述

我想使用关系表上的附加参数和不同的主键来建立 JPA 关系 ManyToMany,但我仍然收到以下错误:有人可以帮我吗?谢谢

您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 2 行的“order INT(11) not null auto_increment, idPoint INT(11), idPolygo”附近使用正确的语法

    @Entity
    @Table(name = "Point")
    public class Point implements Serializable{
               @Id
               @GeneratedValue(strategy=GenerationType.AUTO)
               @Column(name = "idPoint", columnDefinition = "INT(11)", nullable = false)
               private int idPoint;

               @Column(name = "lat", columnDefinition = "DOUBLE(10,8) ", nullable = true)
               private double lat;

               @Column(name = "lng", columnDefinition = "DOUBLE(10,8)", nullable = true)
               private double lng;
                //relationship OneToMany with table PolygonHasPoint
                @OneToMany(mappedBy="point")
                private Set<PolygonHasPoint> polygonHasPoint;
        }
    @Entity
    @Table(name = "Polygon")
    public class Polygon implements Serializable{
            //primary key : idPolygon
            @Id
            @GeneratedValue(strategy=GenerationType.AUTO)
            @Column(name = "idPolygon", columnDefinition = "INT(11)", nullable = false)
            private int idPolygon;

            //relationship OneToMany with table PolygonHasPoint
            @OneToMany(mappedBy="polygon")
            private Set<PolygonHasPoint> polygonHasPoint;
   }

   @Entity(name="Point_Polygon")
   @Table(name = "Polygon_Point")
   public class PolygonHasPoint implements Serializable{

        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name = "order", columnDefinition = "INT(11)", nullable = false)
        private int order;


        @ManyToOne
        @JoinColumn(name = "idPoint", columnDefinition = "INT(11)", nullable = true)
        private Point point;

        @ManyToOne
        @JoinColumn(name = "idPolygon", columnDefinition = "INT(11)", nullable = true)
        private Polygon polygon;
}

标签: javaspringspring-bootjpamariadb

解决方案


'order' 是大多数 RDMBS 中的受限关键字。尝试“位置”或“排序”。

如果您真的想保留当前名称,可以尝试使用反引号进行定义:

@Column(name = "`order`", columnDefinition = "INT(11)", nullable = false)
private int order;

推荐阅读