2011-01-07 2 views
1

ich in meinem application gesetzt habenDataAccessException funktioniert nicht

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

und ich versuche, die DataAccessException in ManagedBean zu fangen. Ich habe BusinessDelegate, wo ich @Transactional eingestellt habe.

Das Problem ist:

try 
    { 
     operazioneOk = businessDelegate.insertAuto(newAuto); 
    } 
    catch (DataAccessException e) 
    { 
     System.out.println("autoBean"); 
    } 

der Fang, Arbeit, selbst wenn ich nicht setzen
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

aber das Merkwürdige ist, dass in der Konsole Ich habe diese Ausnahme:

AVVERTENZA: SQL Error: 0, SQLState: null 
GRAVE: L'operazione «batch» 0 insert into public.auto (marca, modello, anno, km, cilindrata, optional, prezzo, occasione, id) values (w, ww, w, w, w, w, w, 0, 12) è stata interrotta. Chiamare «getNextException» per scoprirne il motivo. 
AVVERTENZA: SQL Error: 0, SQLState: 23505 
GRAVE: ERROR: duplicate key value violates unique constraint "auto_marca_key" 
    Dettaglio: Key (marca)=(w) already exists. 
GRAVE: Could not synchronize database state with session 
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 

In beiden Fällen. Ich denke, dass etwas nicht funktioniert. Hilf mir!

Antwort

2

die Fehler aus der Stapelüberwachung sind

duplicate key value violates unique constraint "auto_marca_key" 
    Dettaglio: Key (marca)=(w) already exists. 

Sie eine Zeile einfügen, die einen Spaltenwert, der eine einzigartige Einschränkung verletzt enthält.

+0

Ich weiß, dass: Duplikatschlüssel ist mein Problem, aber wenn ich Ausnahme erfahre, sehe ich immer den Stack-Trace und mindestens System.out.println ("autoBean"); –

0

Ok, ich habe das Problem selbst gelöst.

Die Lösung setzt die @Transactional Propagation. Diese ist die Methode in meinem BusinessDelegate (SessionFacade):

@Override 
@Transactional 
public boolean insertAuto(Auto auto) 
{ 
    try 
    { 
     return (autoDao.create(auto) != null); 
    } 
    catch (DataAccessException e) 
    { 
     System.out.println("aa"); 
    } 
    return false; 
} 

während in DAO i einstellen:

@Override 
@Transactional(propagation = Propagation.REQUIRES_NEW) 
public PK create(T istance) throws DataAccessException 
{ 
    return (PK) sessionFactory.getCurrentSession().save(istance); 
} 

jetzt alles funktionieren. Wenn jemand irgendein Problem in diesem Ansatz sieht, bitte rate mir.

Verwandte Themen