[英]One-to-Many relationship with ORMLite
我能找到解決這種情況的唯一例子已經很老了,我想知道用最新版本的ORMLite做最好的方法是什么...
說我有兩張桌子(簡化):
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
}
public class Insurance
{
[Alias("InsuranceId")]
[Autoincrement]
public int Id { get; set; }
[ForeignKey(typeof("Patient"))]
public int PatientId { get; set; }
public string Policy { get; set; }
public string Level { get; set; }
}
患者可以在不同的“級別”(主要,次要等)擁有多個保險單。 我理解將保險信息作為字典類型對象填充並將其直接添加到[Patient] POCO的概念,如下所示:
public class Patient
{
public Patient() {
this.Insurances = new Dictionary<string, Insurance>(); // "string" would be the Level, could be set as an Enum...
}
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
public Dictionary<string, Insurance> Insurances { get; set; }
}
public class Insurance
{
public string Policy { get; set; }
}
...但我需要將保險信息作為單獨的表存在於數據庫中,以便稍后用於報告。
我知道我可以在ORMLite中加入這些表,或者在SQL中創建一個聯合的View / Stored Proc來返回數據,但它顯然會為同一個患者返回多行。
SELECT Pat.Name, Ins.Policy, Ins.Level
FROM Patient AS Pat JOIN
Insurance AS Ins ON Pat.PatientId = Ins.PatientId
(Result)
"Johnny","ABC123","Primary"
"Johnny","987CBA","Secondary"
如何將其映射到單個JSON響應對象?
我希望能夠將GET請求映射到“/ patients / 1234”以返回一個JSON對象,如:
[{
"PatientId":"1234",
"Name":"Johnny",
"Insurances":[
{"Policy":"ABC123","Level":"Primary"},
{"Policy":"987CBA","Level":"Secondary"}
]
}]
在單個查詢中,我沒有太多希望可以做到這一點。 它可以用兩個來完成(一個在Patient表上,另一個在Insurance表上)? 如何以嵌套方式將每個查詢的結果添加到同一個響應對象中?
非常感謝您對此的任何幫助!
這就是我所處的位置......在“患者”POCO中,我添加了以下內容:
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
[Ignore]
public List<Insurance> Insurances { get; set; } // ADDED
}
然后,當我想要回復患有多種保險的患者時,我會做兩個問題:
var patientResult = dbConn.Select<Patient>("PatientId = " + request.PatientId);
List<Insurance> insurances = new List<Insurance>();
var insuranceResults = dbConn.Select<Insurance>("PatientId = " + patientResult[0].PatientId);
foreach (patientInsurance pi in insuranceResults)
{
insurances.Add(pi);
}
patientResult[0].Insurances = insurances;
patientResult[0].Message = "Success";
return patientResult;
這有效! 我在保險中使用嵌套項目獲得了不錯的JSON,同時在數據庫中維護了單獨的相關表。
我不喜歡的是這個對象不能來回傳遞給數據庫。 也就是說,我不能使用相同的嵌套對象同時自動插入/更新Patient和InsurancePolicy表。 如果我刪除“[Ignore]”裝飾器,我會在Patient表中獲得一個名為varchar(max)類型的“Insurances”的字段。 不好,對嗎?
我想我需要為我的PUT / POST方法編寫一些額外的代碼來從JSON中提取“Insurances”節點,迭代它,並使用每個Insurance對象來更新數據庫? 我只是希望我不是在這里重新發明輪子或者做更多的工作而不是必要的。
評論仍然會受到贊賞! 是Mythz嗎? :-) 謝謝...
參考資料是為了節省一天!
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
[Reference]
public List<Insurance> Insurances { get; set; }
}
public class Insurance
{
[Alias("InsuranceId")]
[Autoincrement]
public int Id { get; set; }
[ForeignKey(typeof("Patient"))]
public int PatientId { get; set; }
public string Policy { get; set; }
public string Level { get; set; }
}
然后,我可以使用嵌套的“Insurance”數組獲取JSON請求,如下所示:
{
"Name":"Johnny",
"Insurances":[
{"Policy":"ABC123","Level":"Primary"},
{"Policy":"987CBA","Level":"Secondary"}
]
}
...創建一個新記錄並像這樣保存:
public bool Put(CreatePatient request)
{
List<Insurance> insurances = new List<Insurance>();
foreach (Insurance i in request.Insurances)
{
insurances.Add(new Insurance
{
Policy = i.Policy,
Level = i.Level
});
}
var patient = new Patient
{
Name = request.Name,
Insurances = insurances
};
db.Save<Patient>(patient, references:true);
return true;
}
答對了! 我獲得了新的患者記錄,加上保險表中的2條新記錄,正確的外鍵引用回到剛剛創建的PatientId。 這真太了不起了!
另一個更簡潔的例子:
public void Put(CreatePatient request)
{
var patient = new Patient
{
Name = request.Name,
Insurances = request.Insurances.Map(x =>
new Insurance { Policy = i.Policy, Level = i.Level })
};
db.Save<Patient>(patient, references:true);
}
首先,您應該在Patient類中定義外部集合。 (使用get和set方法)
@ForeignCollectionField
private Collection<Insurance> insurances;
當您查詢患者時,您可以通過調用getInsurances方法獲得保險。
要將all轉換為包含數組的單個json對象,可以使用json處理器。 我使用Jackson( https://github.com/FasterXML/jackson )並且效果很好。 下面將以json對象的形式給出一個字符串。
new ObjectMapper().writeValueAsString(patientObject);
要正確映射外部字段,您應該定義jackson引用。 在您的患者類中添加托管引用。
@ForeignCollectionField
@JsonManagedReference("InsurancePatient")
private Collection<Insurance> insurances;
在您的保險類中添加后退參考。
@JsonBackReference("InsurancePatient")
private Patient patient;
更新:您可以使用Jackson從json字符串生成對象,然后迭代並更新/創建數據庫行。
objectMapper.readValue(jsonString, Patient.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.