简体   繁体   中英

object references an unsaved transient instance - save the transient instance before flushing using hibernate spatial

I am triying to execute this query:

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

But I have this exception:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.gisapp.springboot.backend.apirest.models.entity.PolygonEntity

This is the 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;

I have read a possible solution here but watching the entity, the solution has been already implemented in the UserEntity which contains the polygon´s collection:

@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;

Why do I have this exception?

At the end I have used another way to use the within method.

I have created a temporal table and an entity to save the polygon, later I execute this query:

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

The polygon is saved and later is deleted after the use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM