【问题标题】:object references an unsaved transient instance - save the transient instance before flushing using hibernate spatial对象引用未保存的瞬态实例 - 在使用休眠空间刷新之前保存瞬态实例
【发布时间】:2019-07-02 09:09:47
【问题描述】:

我正在尝试执行这个查询:

StringBuffer sb = new StringBuffer();
sb.append("select p from PointsEntity p " + "where within(p.coordinates,:polygon) = true");

但我有这个例外:

org.hibernate.TransientObjectException:对象引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.gisapp.springboot.backend.apirest.models.entity.PolygonEntity

这是多边形实体:

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

@Column(name = "user_id")
private Long userId;

@Column(name = "user_email")
private String userEmail;

@Column(name = "point_name")
private String pointName;

@Column(name = "coordinates")
private Polygon coordinates;

我已经阅读了一个可能的解决方案here,但观察实体,该解决方案已经在包含多边形集合的 UserEntity 中实现:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private List<PointsEntity> pointsList;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private List<PolygonEntity> polygonsList;

为什么会有这个异常?

【问题讨论】:

  • 该错误可能与您显示的查询无关。在同一事务中还进行了哪些其他更改?

标签: java hibernate jts


【解决方案1】:

最后我使用了另一种方法来使用within方法。

我已经创建了一个临时表和一个实体来保存多边形,稍后我执行这个查询:

StringBuffer sb = new StringBuffer();
        sb.append("select p from PointsEntity p, TempPolygonEntity t "
        + "where within(p.coordinates, t.coordinates) = true"); 

多边形被保存,使用后删除。

【讨论】:

    猜你喜欢
    • 2012-02-20
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2015-09-27
    • 2021-03-03
    • 2013-11-30
    相关资源
    最近更新 更多