简体   繁体   English

ORMLite。 如何建立两个相关表的一对一关系,例如“主键到外键”

[英]ORMLite. How to establish one-to-one relations of two tables related, like “primary key to foreign key”

I have two tables, one of them have relations with another over foreign id to primary id. 我有两个表,其中一个与另一个表之间的关系超出了外国ID到主要ID的关系。

And I have some model description, which in my opinion looks correct, but it doesn't work. 而且我有一些模型描述,我认为这看起来是正确的,但它不起作用。

First table 第一桌

public class Person {
    @DatabaseField(id = true)
    private int id;
    @DatabaseField(columnName = "person_id", foreign = true, foreignAutoRefresh = true)
    Patient patient;

    public Patient getPatient() {
        return patient;
    }

Second table 第二张桌子

 public class Patient {
    @DatabaseField(columnName = "person_id", foreign = true)
    private Person personId;

When I try to find data from "Patient" over the "Person", I get a null pointer. 当我尝试通过“人员”从“患者”中查找数据时,我得到了一个空指针。 If change foreign key column name in person, I get PSQLException, because ResultSet does not contain such a column. 如果亲自更改外键列名称,则会得到PSQLException,因为ResultSet不包含这样的列。

For debugging, I use simple main method 对于调试,我使用简单的main方法

public static void main(String[] args) throws SQLException {
    Person p = new PersonDAO().getByUserId();
    System.out.println(p.getPatient());
}

Tables 桌子

How to establish one-to-one relations of two tables related, like “primary key to foreign key” 如何建立两个相关表的一对一关系,例如“主键到外键”

The problem with the 1-to-1 relationship is that in the Person table there is a patientId field and in the Patient table there is a personId field. patientId关系的问题是,在“ Person表中有一个patientId字段,在“ Patient表中有一个personId字段。 But the ids only get set when they are created in the database. 但是,仅在数据库中创建ID时才设置ID。

This means that you will have to do 3 database operations: 这意味着您将必须执行3个数据库操作:

  • Create the Person in the database to get an ID associated with it by calling personDao.create(...) . 通过调用personDao.create(...)在数据库中创建Person以获得与之关联的ID。
  • Set the Person on the corresponding Patient , then create the Patient in the database with patientDao.create(...) . Person设置在相应的Patient ,然后使用patientDao.create(...)在数据库中创建Patient
  • Set the Patient back on the Person entity now that the patient has has an id, then update it in the database with personDao.update(...) . Patient重新站上Person实体现在患者已经有一个id,然后与数据库更新personDao.update(...)

Couple of other things: 其他几件事:

  • I assume you are missing an argument to the dao.getByUserId() method in your post since it needs an id to find the particular one in question. 我假设您在帖子中缺少dao.getByUserId()方法的参数,因为它需要一个ID才能找到有问题的特定对象。
  • I assume that Patient has an id field even though you don't show it in your post. 我假设即使您没有在帖子中显示“ Patient也有一个id字段。
  • The field in the Patient should be just person , not personId . Patient ”中的字段应该只是person ,而不是personId ORMLite will store an id in that field under the covers. ORMLite将在该字段的封面下存储一个ID。 The field in Person is correctly called patient . Person ”中的字段正确地称为“ patient

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM