Buonasera, sto cercando di imparare ad utilizzare spring e hibernate. Ma sto trovando degli ostacoli nella configurazione del context configuration, almeno io penso che il problema si trovi li.
contextConfiguration
daocodice:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.0.xsd "> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSources" /> <property name="mappingResources"> <list> <value>Autore.hbm.xml</value> <value>Genere.hbm.xml</value> <value>Libro.hbm.xml</value> <value>Posizione.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.max_fetch_depth">2</prop> </props> </property> </bean> <bean id="dataSources" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/libreria</value> </property> <property name="username"> <value>libreria</value> </property> <property name="password"> <value>libreria</value> </property> </bean> <bean id="dao" class="stefania.Dao"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> </beans> dao
maincodice:public class Dao { private HibernateTemplate m_hibernateTemplate; public void setSessionFactory(SessionFactory p_sessionFactory) { this.m_hibernateTemplate = new HibernateTemplate(p_sessionFactory); } public List<Libro> listaLibri() { return m_hibernateTemplate.find("from Libro"); } }
poi ci sono i vari bean di genere, libro autore che non posto perchè sono solo getter e setter.codice:public class MainClass { public static void main(String[] args) { Dao e = new Dao(); List<Libro> lista = e.listaLibri(); for (Libro libro : lista) { String output = "id: " + libro.getId() + ", titolo: " + libro.getTitolo() + ", isbn: " + libro.getIsbn(); Set<Autore> autori = libro.getAutore(); for (Autore autore : autori) { output += ", autore: " + autore.getNome(); } Set<Genere> generi = libro.getGenere(); for (Genere genere : generi) { output += ", genere: " + genere.getNome(); } Set<Posizione> posizioni = libro.getPosizione(); for (Posizione posizione : posizioni) { output += ", sala: " + posizione.getSala() + " scaffale: " + posizione.getScaffale(); } output += "."; System.out.println(output); } HibernateUtil.getSessionFactory().close(); } }
i file hbm contenenti le mappature del db sono corrette perchè le ho gia utilizzate e non davano problemi.
Mi viene segnalato un NullPointer alla riga return m_hibernateTemplate.find("from Libro");
eccezione
codice:Exception in thread "main" java.lang.NullPointerException at stefania.Dao.listaLibri(Dao.java:36) at stefania.MainClass.main(MainClass.java:9)
Grazie ciao stefania

Rispondi quotando
