[英]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.