【问题标题】:How to retrieve the auto generated primary key in postgres with ActiveJDBC如何使用 ActiveJDBC 在 postgres 中检索自动生成的主键
【发布时间】:2014-09-20 23:34:46
【问题描述】:

在 ActiveJDBC 中

如果表中有serial primary key,列名id

Employee e = new Employee();
        e.set("name", "John");
        e.set("age", 43);
        e.saveIt();

保存记录后如何检索它? 我想检索以插入该员工的地址:

Address d = new Address();
     d.set("employee_id", ???); // what to do here?
     d.set("address", address);
     d.saveIt();

【问题讨论】:

    标签: java postgresql activejdbc


    【解决方案1】:

    好吧,ActiveJDBC 会自动执行此操作。 下面是正确设置 ID 的相同代码:

    Employee e = Employee.createIt("name", "John", "age", 43);
    Address d = Address.create("address", address);    
    e.add(d);
    

    这是一种简短的写法。更长的版本:

    Employee e = new Employee();
    e.set("name", "John", "age", 43).saveIt();
    Address d = new Address();
    d.set("address", address);
    e.add(d);
    

    在任何一种情况下,您都可以在保存后从模型中获取 ID:

    id = e.getId()
    

    更多信息请参考:http://javalite.io/one_to_many_associations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-12
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多