2012-04-06 21 views
5

Sto cercando di leggere un'entità inizializzata pigra ProductUnits che è un set permanente e ha aperto una nuova sessione per lo stesso.GenericJDBCException: impossibile caricare un'entità

Session session = HibernateUtil.getSessionFactory().openSession(); 
session.update(stock.getProduct()); 
List<ProductUnits> units = new ArrayList<ProductUnits>(stock.getProduct().getUnits()); 

Il problema è durante la fase di esecuzione che genera l'eccezione

org.hibernate.exception.GenericJDBCException: could not load an entity: [hibernate.tables.plain.Product#1] 

ma mentre si fa un passo per passo di debug che gira perfettamente bene. E 'che il Session.update richiede un certo tempo o mi sto perdendo qualcosa

lo stack eccezione è

Exception in thread "AWT-EventQueue-0" org.hibernate.exception.GenericJDBCException: could not load an entity: [hibernate.tables.plain.Product#2] 
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126) 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:1895) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65) 
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072) 
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434) 
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415) 
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165) 
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:121) 
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905) 
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:863) 
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:95) 
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140) 
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) 
at hibernate.tables.plain.Product_$$_javassist_15.getUnits(Product_$$_javassist_15.java) 
at bill.mainBill.InputProductsPanel.computeStockAvailablity(InputProductsPanel.java:278) 
at bill.mainBill.InputProductsPanel.findStock(InputProductsPanel.java:242) 
at bill.mainBill.ProductDetailPanel.setStats(ProductDetailPanel.java:315) 
at bill.mainBill.ProductDetailPanel$3.focusLost(ProductDetailPanel.java:187) 
at java.awt.AWTEventMulticaster.focusLost(Unknown Source) 
at java.awt.Component.processFocusEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$000(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 


    Caused by: java.sql.SQLException: Invalid value for getInt() - 'N' 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2727) 
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2815) 
at org.hibernate.type.IntegerType.get(IntegerType.java:51) 
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184) 
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173) 
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105) 
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2124) 
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404) 
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332) 
at org.hibernate.loader.Loader.getRow(Loader.java:1230) 
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603) 
at org.hibernate.loader.Loader.doQuery(Loader.java:724) 
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:1881) 
... 48 more 

file di allora la mappatura è la seguente

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Apr 3, 2012 1:21:15 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
<class name="hibernate.tables.plain.ProductUnits" table="PRODUCT_UNITS"> 
    <composite-id mapped="false" unsaved-value="undefined"> 
    <key-many-to-one class="hibernate.tables.plain.Product" 
    column="Product_ID" name="product"/> 
    <key-many-to-one class="hibernate.tables.plain.Unit" column="Unit_ID" name="unit"/> 
    </composite-id> 
    <property generated="never" lazy="false" name="basePrice" type="java.lang.Double"> 
    <column name="BASE_PRICE"/> 
    </property> 
</class> 
</hibernate-mapping> 

e la mappatura del prodotto

<class name="hibernate.tables.plain.Product" table="PRODUCT"> 
     <id name="id" type="java.lang.Long"> 
      <column name="ID" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="name" type="java.lang.String"> 
      <column name="NAME" /> 
     </property> 
     <property name="recommendedHolding" type="java.lang.Double"> 
      <column name="RECOMMENDED_HOLDING" /> 
     </property> 
     <set name="units" table="Product_Units" inverse="false" lazy="true"> 
      <key> 
       <column name="Product_ID" /> 
      </key> 
      <one-to-many class="hibernate.tables.plain.ProductUnits" /> 
     </set> 
     <property name="active" type="java.lang.Character"> 
      <column name="ACTIVE" /> 
     </property> 
    </class> 
+1

Sarebbe d'aiuto se ci ha mostrato l'eccezione completo stacktrace –

+0

E, inoltre si prega di fornire Mappatura Hibernate per 'Prodotto'. – nobeh

risposta

2

C'è il tuo problema:

Caused by: java.sql.SQLException: Invalid value for getInt() - 'N' 
+0

Bene, il punto è che non sono nemmeno in grado di capire perché questo sta arrivando non c'è _INT_ datamember nella tabella o nell'entità. Quello probabilmente si riferisce ad un campo 'Caratteristica' è Attivo. non so se MYSQL tratta il carattere come int. –

+0

è la parte del campo "isActive" della tabella PRODUCT_UNITS? –

+0

Nessuna parte della tabella Prodotto. –

3

Sembra che Hibernate abbia un problema nel mappare un valore di carattere N in un java.lang.Boolean.

Se è necessario mappare java.lang.Boolean forse è meglio usare uno o java.lang.Booleanorg.hibernate.type.YesNoType invece di 'java.lang.Character'. Puoi leggere di più nello reference.

Oppure è necessario utilizzare java.lang.Character, seguire lo reference per registrare un tipo personalizzato.

2

benissimo cambiare il tipo di proprietà da personaggio a String

<property name="active" type="java.lang.String"> 
    <column name="ACTIVE" /> 
</property> 
+0

Ho provato questa soluzione alternativa :) –

1

ho trovato un altro riferimento che indica che Hibernate getta un personaggio ad un int (anche se il carattere dello stato documentazione è un tipo di dati utilizzabili ...). Le altre risposte dovrebbero risolvere questo. O ti consiglierei di usare solo un varchar (1) invece - lo stesso risultato finale.

cioè)

@Column(name = "column", nullable = false, length = 1) 
public String getColumn() { 
    return column; 
} 

o

<property name="active" type="java.lang.String"> 
     <column name="ACTIVE" length="1"/> 
    </property> 
2

Ho il sospetto che il problema è Sfilettatrice dalle impostazioni di mappatura:

<composite-id mapped="false" unsaved-value="undefined"> 

Nota:

unsaved-value (opzionale - il default è un valore "ragionevole"): un valore di proprietà identificatore che indica un'istanza è appena istanziato (salvato), distinguendola da istanze indipendenti che state salvate o caricate in una sessione precedente .

L'impostazione sta causando il mancato mantenimento di PRODUCT_UNITS.

L'attributo unsaved-value non è quasi mai necessario in Hibernate3.

+0

Ma ancora non risponde perché funziona correttamente durante il debug. –

0

per favore, controllare file persistence.xml aggiungere le classi appropriate Nota: -Data Fonte di file persistence.xml non è correttamente abbinato

+0

Questo dovrebbe essere un commento, non una risposta. Controlla questa [metaSO question] (http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) e [Jon Skeet: Coding Blog] (http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx) su come dare una risposta corretta. – Yaroslav

Problemi correlati