2011-11-14 10 views
6

Ho installato jOOQ in eclissi, ho generato classi per my mySQL, ma ho ancora problemi a scrivere anche alcune query di base.jOOQ inserisci query con chiavi generate di ritorno

ho cercato di comporre query di inserimento, con il ritorno di chiavi generate, ma il compilatore genera un errore

Tabella: tblCategory Colonne: category_id, parent_id, nome, rem, uipos

Result<TblcategoryRecord> result= create.insertInto(Tblcategory.TBLCATEGORY, 
    Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS) 
     .values(node.getParentid()) 
     .values(node.getName()) 
     .values(node.getRem()) 
     .values(node.getUipos()) 
     .returning(Tblcategory.CATEGORY_ID) 
     .fetch(); 

cercarono anche altri differente modi come fare nel modo giusto?

grazie charis

risposta

11

La sintassi che si sta utilizzando è per l'inserimento di più record. Questo inserirà 4 record, ognuno con un campo.

.values(node.getParentid()) 
.values(node.getName()) 
.values(node.getRem()) 
.values(node.getUipos()) 

Ma avete dichiarato 4 campi, in modo che non sta andando a lavorare:

create.insertInto(Tblcategory.TBLCATEGORY, 
    Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS) 

Quello che probabilmente vuole fare è questo:

Result<TblcategoryRecord> result = create 
    .insertInto(Tblcategory.TBLCATEGORY, 
    Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS) 
    .values(node.getParentid(), node.getName(), node.getRem(), node.getUipos()) 
    .returning(Tblcategory.CATEGORY_ID) 
    .fetch(); 

O in alternativa:

Result<TblcategoryRecord> result = create 
    .insertInto(Tblcategory.TBLCATEGORY) 
    .set(Tblcategory.PARENT_ID, node.getParentid()) 
    .set(Tblcategory.NAME, node.getName()) 
    .set(Tblcategory.REM, node.getRem()) 
    .set(Tblcategory.UIPOS, node.getUipos()) 
    .returning(Tblcategory.CATEGORY_ID) 
    .fetch(); 

Probabilmente, sei pari meglio utilizzando

TblcategoryRecord result = 
    // [...] 
    .fetchOne(); 

Per maggiori dettagli, si consideri il manuale:

http://www.jooq.org/doc/2.6/manual/sql-building/sql-statements/insert-statement/

O il Javadoc per la creazione di INSERT dichiarazioni che restituiscono valori:

http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html

+0

grazie, ha aggiunto soluzione preferita, ma ha ancora bisogno di Eclipse colata – Charis997

+0

@ Charis997: Di solito non è raccomandato di mettere risposte a domande su Stack Overflow. Sarà difficile per i visitatori successivi capire cosa sta succedendo. Invece, puoi "accettare" questa risposta o fornire una risposta tu stesso, se questo non è abbastanza chiaro –

+0

@ Charis997: Informazioni sul casting: hai ragione. Ciò non sarà più necessario con jOOQ 2.0. Ma è ancora necessario con jOOQ 1.6.9 –

3

preferito per il tuo SOLUZIONE

try { 
    TblcategoryRecord record = (TblcategoryRecord) create 
     .insertInto(Tblcategory.TBLCATEGORY) 
     .set(Tblcategory.PARENT_ID, node.getParentid()) 
     .set(Tblcategory.NAME, node.getName()) 
     .set(Tblcategory.REM, node.getRem()) 
     .set(Tblcategory.UIPOS, node.getUipos()) 
     .returning(Tblcategory.CATEGORY_ID) 
     .fetchOne(); 

     node.setId(record.getCategoryId()); 

    } catch (SQLException e1) { } 
+4

Non dovresti davvero ignorare quella SQLException! Se è solo un esempio, puoi lasciare tutto il blocco try/catch. –

0

Prova

YoutableRecord result = create 
.insertInto(YOURTABLE) 
.set(YOURTABLE.PROD_NAME, "VAL") 
.returning(YOURTABLE.ID_PR) 
.fetchOne(); 


int id = result.getValue(Products.PRODUCTS.ID_PR); 
Problemi correlati