25

I am playing with dropwizard and I want to build a REST application that has various foreign-key relations in the entities.

For example given the following 3 tables:

-- table persons
CREATE TABLE PUBLIC.PERSONS(
    ID BIGINT DEFAULT NOT NULL AUTOINCREMENT,
    FIRST_NAME VARCHAR(255),
    LAST_NAME VARCHAR(255),
    BIRTHDAY DATE,
    ADDRESS_ID BIGINT NOT NULL,
    CREATED TIMESTAMP DEFAULT 'current_timestamp',
    MODIFIED TIMESTAMP
);
ALTER TABLE PUBLIC.PERSONS ADD CONSTRAINT PUBLIC.PK_PERSONS PRIMARY KEY(ID);

-- table customers
CREATE TABLE PUBLIC.CUSTOMERS(
    ID BIGINT DEFAULT NOT NULL AUTOINCREMENT,
    PERSON_ID BIGINT NOT NULL,
    STATUS_CODE VARCHAR(100) DEFAULT 'ACQUISITION' NOT NULL,
    CREATED TIMESTAMP DEFAULT 'current_timestamp',
    MODIFIED TIMESTAMP
);
ALTER TABLE PUBLIC.CUSTOMERS ADD CONSTRAINT PUBLIC.PK_CUSTOMERS PRIMARY KEY(ID);

-- table addresses
CREATE TABLE PUBLIC.ADDRESSES(
    ID BIGINT DEFAULT NOT NULL AUTOINCREMENT,
    LINE_1 VARCHAR(255),
    LINE_2 VARCHAR(255),
    ZIP VARCHAR(255),
    CITY VARCHAR(255),
    COUNTRY_CODE VARCHAR(3),
    PHONE VARCHAR(255),
    FAX VARCHAR(255),
    CREATED TIMESTAMP DEFAULT 'current_timestamp',
    MODIFIED TIMESTAMP
);
ALTER TABLE PUBLIC.ADDRESSES ADD CONSTRAINT PUBLIC.PK_ADDRESSES PRIMARY KEY(ID);

-- and following forign key constraints:
ALTER TABLE PUBLIC.PERSONS ADD CONSTRAINT 
    PUBLIC.FK_PERSON_ADDRESS FOREIGN KEY(ADDRESS_ID) 
    REFERENCES PUBLIC.ADDRESSES(ID) ON DELETE SET NULL NOCHECK;

ALTER TABLE PUBLIC.CUSTOMERS ADD CONSTRAINT 
    PUBLIC.FK_CUSTOMER_PERSON FOREIGN KEY(PERSON_ID) 
    REFERENCES PUBLIC.PERSONS(ID) ON DELETE SET NULL NOCHECK;

I have started to implement a customerDAO that reads the data of the customer table an dthe referenced tables with one SQL query, that was not very complicated:

@RegisterMapper(CustomerResultMapper.class)
public interface CustomerDAO {

    @SqlQuery("select p.id as person_id, "
            + "p.first_name, p.last_name, p.birthday, "
            + "c.id as customer_id, c.status_code, "
            + "a.id as address_id, a.line_1, a.line_2, a.zip, "
            + "a.city, a.country_code, a.phone, a.fax "
            + "from customers c, persons p, addresses a "
            + "where c.id = :id "
            + "and c.person_id = p.id "
            + "and p.address_id = a.id")
    public Customer findCustomerById(@Bind("id") long id);
}

(For the sake of brevity, I skip the mapper, since that is not my actual question)

Now I want to insert a new customer, I have all the required data, including the data which belongs in the referenced tables.

I could not find a way how to execute multiple queries with jdbi annotations, so I figured, I have to create a DAO method for every one of the tables and insert the data from within java, updating the foreign key references manually.

But also this does not work because I could not find a way to read the autogenerated ID value after an insert.

Any idea how I could approach this problem so I can keep the references correct?

4

2 回答 2

42

没关系,我同时找到了解决方案,至少如何获取自动生成的密钥:

在您的 DAO 中,添加 @GetGeneratedKeys 注释,确保您的方法的返回值与生成的键的值匹配:

@SqlUpdate("insert into addresses (line_1) values ('address line 1')")
@GetGeneratedKeys
public long insertAddress();

然后,在您的资源中,您可以简单地使用返回值:

long id = customerDAO.insertAddress();
System.out.println("Found id " + id);

这可以为一个语句获取生成的密钥,但是在我的情况下,我仍然需要进行多个查询并手动填写正确的引用。

如果有人知道如何简化这个过程并让参考文献自动填写,我仍然很想听听。

谢谢。

于 2014-10-01T16:39:31.847 回答
23

使用 postgres,您不仅可以获得一个,甚至可以获得所有自动生成的值。

您可以通过将 @SqlUpdate 更改为 @SqlQuery 并使用 INSERT ... RETURNING * 来做到这一点,假设您有一个地址映射器:

@SqlQuery("insert into addresses (line_1) values ('address line 1') returning *")
public Address insertAddress(@BindBean Address address);
于 2015-09-24T14:59:21.410 回答