我的实体管理器有问题,它返回 null。我已经配置persistence.xml
并且applicationContext.xml
当我创建一个测试类时,Junit 工作并在数据库中创建表。
但是当我尝试使用 jsf 插入时,实体管理器总是返回 null。这是一个带有 hibernate 和 jsf 的 spring 项目,这是我的类 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;
}
}
这是我的道课
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);
}
}
这是页面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>
这是我的 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>