我正在使用hibernate,stripersist和jpa开发条带样本应用程序。我在访问数据库时遇到错误。我是条纹和冬眠的新手。 错误如下
org.apache.jasper.JasperException: javax.el.ELException: Error reading 'contacts' on
type com.action.ContactListActionBean
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:54 9)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.el.ELException: Error reading 'contacts' on type com.action.ContactListActionBean
javax.el.BeanELResolver.getValue(BeanELResolver.java:110)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
org.apache.jsp.WEB_002dINF.jsp.contact_005flist_jsp._jspService(contact_005flist_jsp.jav a:125)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
java.lang.NullPointerException
com.dao.impl.stripersist.BaseDaoImpl.read(BaseDaoImpl.java:36)
com.action.ContactListActionBean.getContacts(ContactListActionBean.java:46)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
javax.el.BeanELResolver.getValue(BeanELResolver.java:99)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluenter code
hereate(PageContextImpl.java:967)
org.apache.jsp.WEB_002dINF.jsp.contact_005flist_jsp._jspService(contact_005flist_jsp.jav a:125)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
BaseDaoImpl.java
public abstract class BaseDaoImpl<T,ID extends Serializable>
implements Dao<T,ID>
{
private Class<T> entityClass;
@SuppressWarnings("unchecked")
public BaseDaoImpl() {
entityClass = (Class<T>)
((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public List<T> read() {
return Stripersist.getEntityManager()
.createQuery("from " + entityClass.getName())
.getResultList();
}
public T read(ID id) {
return Stripersist.getEntityManager().find(entityClass, id);
}
@SuppressWarnings("unchecked")
public void save(T object) {
Stripersist.getEntityManager().persist(object);
}
public void delete(T object) {
Stripersist.getEntityManager().remove(object);
}
public void commit() {
Stripersist.getEntityManager().getTransaction().commit();
}
@SuppressWarnings("unchecked")
private T getSingleResult(Query query) {
try {
return (T) query.getSingleResult();
}
catch (NonUniqueResultException exc) {
return (T) query.getResultList().get(0);
}
catch (NoResultException exc) {
return null;
}
}
}
ContactDao.java
public interface ContactDao extends Dao<Contact,Integer> {
}
ContactDaoImpl.java
public class ContactDaoImpl extends BaseDaoImpl<Contact,Integer>
implements ContactDao
{
}
Contact.java
@Entity
public class Contact extends ModelBase{
private String firstName;
private String lastName;
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
ModelBase.java
@MappedSuperclass
public class ModelBase {
@Id
@GeneratedValue
private Integer id;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Override
public boolean equals(Object obj) {
try { return id.equals(((ModelBase) obj).getId()); }
catch (Exception exc) { return false; }
}
@Override
public int hashCode() {
return 31 + ((id == null) ? 0 : id.hashCode());
}
}
ContactListActionBean.java
public class ContactListActionBean extends BaseActionBean{
private static final String LIST="/WEB-INF/jsp/contact_list.jsp";
@DefaultHandler
public Resolution list() {
return new ForwardResolution(LIST);
}
public List<Contact> getContacts() {
return contactDao.read();
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.model.Contact</class>
<properties>
<!-- Autodetect entity classes -->
<property name="hibernate.archive.autodetection" value="class"/>
<!-- Automatically create the SQL schema -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<!-- Tell Hibernate to use HSQLDB -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- Configure the JDBC database connection -->
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/webstore"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="jdbc.batch_size" value="0"/>
<!-- Configure the connection pool -->
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
</properties>
</persistence-unit>
</persistence>
任何解决方案吗?
答案 0 :(得分:0)
您添加的堆栈跟踪在BaseDaoImpl行36上显示NullPointerException。可能在Hibernate / JPA(persistence.xml)中未正确配置EntityManager。