Spring + Hibernate sessionFactory注入不起作用

时间:2016-07-18 12:06:59

标签: java spring hibernate

您好我是Spring框架和Hibernate框架的初学者。我正在尝试集成一个宁静的spring项目hibernate框架。 我有一个控制器类来管理请求,一个模型类映射到DB与JPA和DAO类,实现查询与mysql DB。 型号类:

@Entity
@Table(name="paziente")
public class Paziente {
    @EmbeddedId
    private PazientePK pazientePK;

    @Column(name = "Nome")
    String Nome;

    @Column(name = "Cognome")
    String Cognome;

    @Column(name = "Sesso")
    String Sesso;

    @Column(name = "Indirizzo")
    String Indirizzo;

    @Column(name = "Paese")
    String Paese;

    @Column(name = "Provincia")
    String Provincia;

    @Column(name = "DataNascita")
    Date DataNascita;

    public PazientePK getPazientePK() {
        return pazientePK;
    }
    public void setPazientePK(PazientePK pazientePK) {
        this.pazientePK = pazientePK;
    }
    //getters and setters
}


@Embeddable
public class PazientePK implements Serializable{
    @Column(name="ID")
    protected int ID;

    @Column(name="CF")
    protected String CF;

    public PazientePK(int ID, String cf){
        this.ID = ID;
        this.CF = cf;
    }


}
道上课:

public class PazienteDAO {
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public Paziente Insert(String nome, String cognome, String cf, String sesso,     String indirizzo,
        String paese, String provincia, Date dataNascita){

    try{
        /*
        HibernateUtil hu = new HibernateUtil();
        Session session = hu.getSessionFactory().openSession();
        */
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Paziente utente = new Paziente();
        utente.setPazientePK(new PazientePK(1,cf));
        utente.setNome(nome);
        utente.setCognome(cognome);
        utente.setSesso(sesso);
        utente.setIndirizzo(indirizzo);
        utente.setPaese(paese);
        utente.setProvincia(provincia);
        utente.setDataNascita(dataNascita);

        session.save(utente);
        session.getTransaction().commit();
        return utente;
    }catch(Exception e){
        e.printStackTrace();
        return null;
        }

    }
}

控制器类:

@RestController
public class PazienteController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/paziente")
public Paziente paziente(@RequestParam(value="name", defaultValue="World") String name) {

    PazienteDAO pazienteDao = new PazienteDAO();
    Paziente utente = pazienteDao.Insert("aaa", "bbb", "abdc", "M", "asd", "adf", "ad", new Date(5,5,1895));
    return utente;
}
   }

最后是spring配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"    
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"

 xsi:schemaLocation=
 "http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan 
    base-package="com.smr" />
<context:annotation-config></context:annotation-config>
<tx:annotation-driven/>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/SMR" />
    <property name="username" value="Luigi" />
    <property name="password" value="password" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
        <list>
            <value>com.smr.app.Paziente</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="debug">true</prop>
        </props>
    </property>
</bean>
<bean id="myPazienteDao" class="com.smr.app.PazienteDAO">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionManager"    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
 </bean>
</beans>

问题出现在dao类中:

Session session = sessionFactory.openSession();

我有错误NullPointerException,sessionFactory变量为null。 任何人都可以帮我解决这个问题吗? 非常感谢。

0 个答案:

没有答案