Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

java - Hibernate JPA填充瞬态或公式字段(Hibernate JPA Populating transient or formula field)

I have an optional column distance in entity class.

(我在实体类中有一个可选的列distance 。)

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.

(使它@transient在获取时不会被休眠映射。)

SqlResultSetMapping is not working either.

(SqlResultSetMapping也不起作用。)

I tried to use @Formula but there is some input values in native query.

(我尝试使用@Formula但本机查询中有一些输入值。)

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

(这是Post DAO类的方法)

// 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
" +
        "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
  ask by Prakash13 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...