与ORMLite的一对多关系

时间:2014-04-28 13:23:16

标签: sql json ormlite ormlite-servicestack

我能找到的解决这种情况的唯一例子已经很老了,我想知道用最新版本的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请求映射到&#34; / patients / 1234&#34;返回一个JSON对象,如:

[{
   "PatientId":"1234",
   "Name":"Johnny",
   "Insurances":[
      {"Policy":"ABC123","Level":"Primary"},
      {"Policy":"987CBA","Level":"Secondary"}
   ]
}]

在单个查询中,我没有太多的希望可以做到这一点。它可以用两个来完成(一个在Patient表上,另一个在Insurance表上)?如何以这种嵌套方式将每个查询的结果添加到同一个响应对象中?

非常感谢你们提供任何帮助!

更新 - 2014年4月29日

我在这里......在病人&#34; 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表。如果我删除&#34; [忽略]&#34;装饰员,我在患者表中得到了一个名为&#34; Insurances&#34; varchar(max)类型。不好,对吧?

我想我需要为我的PUT / POST方法编写一些额外的代码来提取&#34;保险&#34;来自JSON的节点,迭代它,并使用每个Insurance对象来更新数据库?我只是希望我不在这里重新发明轮子,或者做更多的工作而不是必要的工作。

评论仍然会受到赞赏!是Mythz吗? :-)谢谢...

3 个答案:

答案 0 :(得分:1)

参考资料是为了节省一天!

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。太棒了!

答案 1 :(得分:1)

另一个更简洁的例子:

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);
}

答案 2 :(得分:0)

首先,您应该在Patient类中定义一个外部集合。 (使用get和set方法)

@ForeignCollectionField
private Collection<Insurance> insurances;

当您查询患者时,您可以通过调用getInsurances方法获得保险。

要将all转换为内部数组的单个json对象,可以使用json处理器。我使用杰克逊(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);
相关问题