我在使用dropwizard,我想構建一個在實體中具有各種外鍵關係的REST應用程序。jdbi在插入時返回自動生成的值
例如給出如下3個表:
-- 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;
我已經開始實施customerDAO讀取客戶表中的D出現引用同一個SQL查詢表的數據,這是不是很複雜:
@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);
}
(爲簡便起見,我跳過映射器,因爲這不是我的實際問題)
現在我要插入一個新客戶,我擁有所有必需的數據,包括屬於參考表格中的數據。
我無法找到一個方法如何使用jdbi註釋執行多個查詢,所以我想,我必須爲每個表創建一個DAO方法,並從java中插入數據,手動更新外鍵引用。
但這也不起作用,因爲我找不到插入後讀取自動生成的ID值的方法。
任何想法我可以如何解決這個問題,所以我可以保持引用正確?
您是否嘗試過製作方法返回一個列表? –
2014-10-07 03:30:27
不,據我瞭解,GetGeneratedKeys Annotation返回值asint或long,你不能返回一個列表。 – 2014-10-09 00:24:58
看着org.skife.jdbi.v2.GeneratedKeys,它可能會起作用。代碼調用返回ResultSet的java.sql.Statement.getGeneratedKeys()。試一試。 – 2014-10-09 17:49:49