0

I have an optional column distance in entity class. Its not always required to be populated but for some special fetch queries only. Making it @transient will not be mapped by hibernate in fetching. SqlResultSetMapping is not working either. I tried to use @Formula but there is some input values in native query.

This is Post entity class

@Entity
@Table(name = "post")
public class Post {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @JsonProperty("id")
     @Column(name = "post_id")
     private int postId;

     @JsonIgnore
     @Transient
     @Column(name = "distance", insertable=false, updatable=false)
     private Double distance;

     // Other columns and setters getters
}

This is Post service class method

@Override
public List<Post> findPostsForUpdatedFeed(Integer id, Double latitude, Double longitude) {
     if(latitude != null && longitude != null)
          return postDao.findPostsForUpdatedFeed(id, latitude, longitude);
     else
          return postDao.findPostsForUpdatedFeedWithoutLatLong(id);
}

This is Post DAO class methods

// Actual query uses subqueries, joins and union
@Query(value = "(select distinct p.*,(3959 * acos (cos ( radians(:latitude) )* cos( radians( p.post_latitude ) )* cos( radians( p.post_longitude ) - radians(:longitude) )+ sin ( radians(:latitude) )* sin( radians( p.post_latitude ) ) ) ) AS distance\n" +
        "from post p where id=:id;", nativeQuery = true)
List<Post> findPostsForUpdatedFeed(@Param("id") Integer id, @Param("latitude") Double latitude, @Param("longitude") Double longitude);

// unlike above query, distance is not calculated here
@Query(value = "(select distinct p.* from post p where id=:id;", nativeQuery = true)
List<Post> findPostsForUpdatedFeedWithoutLatLong(@Param("id") Integer id);

// There are 3 more quries and 2 of them requires distance to be calculated
Prakash13
  • 91
  • 1
  • 9
  • Remove the `@Column` annotation from the `distance` mapping field – Marco R. Dec 01 '19 at 08:15
  • Marco. I tried removing @Column as well, looks like it doesn't affect as long as distance is transient. – Prakash13 Dec 01 '19 at 08:20
  • What you want is to use the Transient annotation to ignore the field. I am not sure if you can dynamically include or exclude an annotation for a field. Now the reason that Transient may not work with your code probably has to do with the relative package you included, try to set as Transient your getter instead of the property and take a look here https://stackoverflow.com/questions/4662582/make-hibernate-ignore-class-variables-that-are-not-mapped – Christos Karapapas Dec 01 '19 at 08:31
  • ChristosKarapapas Unfortunately setting @transient at getter getDistance() didn't work. I get java.sql.SQLSyntaxErrorException: Unknown column 'post0_.distance' in 'field list' error when mappedBy tried to populate the post from within. – Prakash13 Dec 01 '19 at 08:43
  • You could use [the lazy loading for an entity attribute](https://vladmihalcea.com/the-best-way-to-lazy-load-entity-attributes-using-jpa-and-hibernate/). – SternK Dec 01 '19 at 15:30

0 Answers0