lundi 30 mars 2015

Nullable entity manager using JSF, Spring and Hibernate

I have a problem with the entity manager, it returns null. I have configured persistence.xml and applicationContext.xml and when I create a test class the Junit works and create the tables in the database.


But when I try do insert with jsf the entity manager returns always null. It's a spring project with hibernate and jsf and this is the code source of my class bean



package net.sid.eboutique.metier;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.sid.eboutique.entities.Utilisateur;
import net.sid.eboutique.service.UserDao;

@ManagedBean
@SessionScoped
public class UserBean {

private UserDao managerDao;

public Utilisateur user;

public UserBean(){
managerDao = new UserDao();
}

public void creerUser(){
managerDao.creerUser(user);
}

public Utilisateur getUser() {
return user;
}

public void setUser(Utilisateur user) {
this.user = user;
}

}


and this is my daoclass



package net.sid.eboutique.service;

import static org.junit.Assert.assertTrue;
import net.sid.eboutique.entities.Utilisateur;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class UserDao {

private Utilisateur user;

@PersistenceContext
public EntityManager em;

public void creerUser(Utilisateur user) {
em.persist(user);
}

}


and this is the page xhtml



<h:form>
<h:panelGrid columns="2">

<h:outputLabel value="Nom" for="name" />
<h:inputText id="name" value="#{userBean.user.nom}" />
<h:outputLabel value="Email" for="mail" />
<h:inputText id="mail" value="#{userBean.user.email}" />
<h:outputLabel value="Telephone" for="tel" />
<h:inputText id="tel" value="#{userBean.user.tel}" />
<h:outputLabel value="Adresse" for="adresse" />
<h:inputText id="adresse" value="#{userBean.user.adresse}" />
</h:panelGrid>

<h:form>
<h:commandButton action="#{userBean.creerUser}" value="Save" />
</h:form>
</h:form>


this is my applicationContext.xml :



<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eboutique"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="datasource"></property>
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
<property name="persistenceUnitName" value="UN_EBOUTIQUE"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config></context:annotation-config>

Aucun commentaire:

Enregistrer un commentaire