通过PagingAndSortingRepository在Spring Data Rest中的@Transient列上排序

时间:2018-06-25 06:14:32

标签: spring spring-boot spring-data-jpa spring-data spring-data-rest

我们的应用程序使用PagingAndSortingRepository服务于我们的REST API。这很好用,但是我们遇到了一个似乎无法解决的特殊情况: 我们有一个必须可排序的字母数字字段(例如SOMETHING-123)。一种可能的解决方案是在数据库查询的order by中使用诸如正则表达式之类的东西。由于我们希望保持数据库独立,因此这被排除。因此,我们将该列分为两列。

因此,在我们创建具有1个字符串字段的实体之前:

PatientVC

现在,我们有了一个带有2个附加字段的实体,并在@PostLoad中填充了旧字段@Transient:

import UIKit

import MaterialComponents.MaterialSnackbar

class EncounterFilterTableViewCell: UITableViewCell{

    @IBOutlet weak var filterCheckBox: CheckBox!
    @IBOutlet weak var filterNameTextView: UITextView!

    var filterIndex : [String : Int] = [
        getDataFromAppLanguage("Final") + " " + getDataFromAppLanguage("Diagnosis") : 0,
        getDataFromAppLanguage("Diagnosis") : 1,
        getDataFromAppLanguage("Test") : 2,
        getDataFromAppLanguage("Operation") : 3,
        getDataFromAppLanguage("Drug") : 4,
        getDataFromAppLanguage("Media") : 5,
        getDataFromAppLanguage("FormsEn") : 6,
        getDataFromAppLanguage("PatientEn") + " " + getDataFromAppLanguage("Status") : 7
    ]
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @IBAction func filterBoxClicked(_ sender: AnyObject) {
        EncounterFilterViewController.updateFilterStatus(filterIndex[filterNameTextView.text]!)
    }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
        let message = MDCSnackbarMessage()

        let action = MDCSnackbarMessageAction()
        let actionhandler = {() in
           let actionmessage = MDCSnackbarMessage()
            actionmessage.text = "Button Click Success"
           MDCSnackbarManager.show(actionmessage)
             let storyboard = UIStoryboard(name: "Main", bundle: nil);
        let vc = storyboard.instantiateViewController(withIdentifier: "PatientVC") as! PatientViewController; 
        self.navigationController?.pushViewController(vc, animated: true);            
        }
        action.handler = actionhandler
        action.title = "Done"
        message.action = action





        message.text = "Welcome"
        MDCSnackbarManager.show(message)

    }

}

这很好用,其他字段也不会暴露。但是,对字段“字母数字”的排序显然不再起作用。最简单的解决方案是发出此请求:

@Entity
public class TestingEntity {
    @Id
    @GeneratedValue
    private long id;

    private String alphanumeric
}

并将其内部重写为工作请求:

@Entity
public class Testing {
    @Id
    @GeneratedValue
    private long id;

    @Transient
    public String alphanumeric;

    @PostLoad
    public void postLoad(){
        this.alphanumeric = this.alphabetic + "-" + this.numeric;
    }

    public void setAlphanumeric(String alphanumeric) {
        int index = alphanumeric.indexOf("-");
        this.alphabetic = alphanumeric.substring(0, index);
        this.numeric = Long.parseLong(alphanumeric.substring(index + 1));
    }

    @JsonIgnore
    private String alphabetic;

    @JsonIgnore
    private Long numeric;

}

解决此问题的最佳方法是什么?

0 个答案:

没有答案