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
338 views
in Technique[技术] by (71.8m points)

java - optional jpa property based on if is present in table for jpa entity

i have this entity

public class Shop implements Serializable {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String shop_name;
    private String shop_street_address;
    private String shop_zip;
    private String shop_state;
    private double SHOP_LATITUDE;
    private double SHOP_LONGITUDE;
    private double distance;

    public Shop(String shop_name, String shop_street_address, String shop_zip, String shop_state, double SHOP_LATITUDE, double SHOP_LONGITUDE) {
        this.shop_name = shop_name;
        this.shop_street_address = shop_street_address;
        this.shop_zip = shop_zip;
        this.shop_state = shop_state;
        this.SHOP_LATITUDE = SHOP_LATITUDE;
        this.SHOP_LONGITUDE = SHOP_LONGITUDE;
    }

    public Shop() {
    }



}

i also have this entity with getProximalShops statement that works just fine

@Repository
public interface ShopRepository extends CrudRepository<Shop, Integer> {


    @Query(
            value =
    "SELECT *, " +
            "( 3959 * acos ( cos ( radians(?1)) * cos( radians( SHOP_LATITUDE ) ) * cos( radians( SHOP_LONGITUDE ) - radians(?2) ) + sin ( radians(?1) ) * sin( radians( SHOP_LATITUDE )) ) ) " +
            "AS DISTANCE FROM SHOP_INFORMATION GROUP BY ID HAVING DISTANCE < 2000 ORDER BY DISTANCE ASC LIMIT 0, 20",
    nativeQuery = true)
    List<Shop> getProximalShops(@Param("user_latitude") Double userlatitude,@Param("user_longitude") Double userlongitude);
}

    List<Shop> getProximalShops(@Param("user_latitude") Double userlatitude,@Param("user_longitude") Double userlongitude);

this is my schema:

    CREATE TABLE shop_information (
  id INT AUTO_INCREMENT  PRIMARY KEY,
  shop_name VARCHAR(100) NOT NULL,
  shop_street_address VARCHAR(100) NOT NULL,
  shop_zip VARCHAR(10) NOT NULL,
  shop_state VARCHAR(30) NOT NULL,
  SHOP_LATITUDE DOUBLE NOT NULL,
  SHOP_LONGITUDE DOUBLE NOT NULL
);

but when i try to do a regular findall(select * from jpa) it errors because it cant find distance in the table since its not available in the database, the only time distance is available is when calculated with the getProximalShops query and generated on the fly, i dont need to define it in my original schema.

is there a way to define it as optional preferably only set it when the db has that column? ive tried @transient but distance just doesn't get set when using getProximalShops but the findall(select * from jpa) statement works.


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

1 Reply

0 votes
by (71.8m points)

You can provide a Query for the findAll() method, and include an appropriate 'empty' value for distance in that query.

Since you have defined distance as a primitive double, you'd have to provide an actual double, but that might be confusing. You might want to change that to a Double object, and return null from the findAll() query.

Oh, and BTW, you probably should not use "select *", and should explicitly name the columns. While it frequently works as you would expect, there's actually no guarantee on the order of columns returned from "select *".


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

...