0

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?

Vimukthi
  • 846
  • 6
  • 19
Carlos FTG
  • 81
  • 1
  • 12
  • The error probably has nothing to do with the query you are showing. What other changes are made within the same transaction? – Tobb Jul 02 '19 at 09:31

1 Answers1

0

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.

Carlos FTG
  • 81
  • 1
  • 12