问题描述
最近在学习hibernate框架,测试代码中没有出现过java.lang.reflect.InvocationTargetException这个异常,正常运行。
但是,在为web项目下,出现了java.lang.reflect.InvocationTargetException异常
问题出现的环境背景及自己尝试过哪些方法
Myeclipse10,tomcat7,hibernate5
一开始在百度上找解决方法,有人是导jar包导错了https://blog.csdn.net/u010652...。但是很明显,我的问题并不是jar包的位置问题
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
//CustomerDao
public class CustomerDao implements ICustomerDao {
@Override
public boolean addCustomer(Customer cust) {
boolean flag=false;
Session s = Dbutil.getSession();
Transaction ts = s.beginTransaction();
s.save(s);
ts.commit();
s.close();
flag=true;
return flag;
}
}
//实体类Customer
public class Customer extends Father{
private static final long serialVersionUID = 1L;
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
private String cust_info;
public Customer() {}
public Customer(Long cust_id, String cust_name, Long cust_user_id,
Long cust_create_id, String cust_source, String cust_industry,
String cust_level, String cust_linkman, String cust_phone,
String cust_mobile, String cust_info) {
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_user_id = cust_user_id;
this.cust_create_id = cust_create_id;
this.cust_source = cust_source;
this.cust_industry = cust_industry;
this.cust_level = cust_level;
this.cust_linkman = cust_linkman;
this.cust_phone = cust_phone;
this.cust_mobile = cust_mobile;
this.cust_info = cust_info;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
public String getCust_info() {
return cust_info;
}
public void setCust_info(String cust_info) {
this.cust_info = cust_info;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name
+ ", cust_user_id=" + cust_user_id + ", cust_create_id="
+ cust_create_id + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level="
+ cust_level + ", cust_linkman=" + cust_linkman
+ ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile
+ ", cust_info=" + cust_info + "]";
}
}
//customerServlet方法
public class customerServlet extends BaseServlet{
private static final long serialVersionUID = 1L;
public void addsubmit(HttpServletRequest request, HttpServletResponse response){
String param1 = request.getParameter("cust_name");
String param2 = request.getParameter("cust_level");
Customer cust=new Customer();
cust.setCust_name(param1);
cust.setCust_level(param2);
new CustomerDao().addCustomer(cust);
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
代码正常运行(上面的代码在测试例子中是没问题的)
错误信息:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at Servlet.BaseServlet.service(BaseServlet.java:20)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at Dao.impl.CustomerDao.addCustomer(CustomerDao.java:14)
at Servlet.customerServlet.addsubmit(customerServlet.java:22)
... 23 more
Caused by: org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : Entity/customer.hbn.xml : origin(Entity/customer.hbn.xml)
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at Dao.Dbutil.(Dbutil.java:12)
... 25 more