求助!myeclipise调用自动生成的DAO出错

u010162389 2015-04-14 01:15:37
自己写了一个简单的SSH框架实例 就是把jsp表单里的数据存到数据库(mysql)里;其中DAO和pojo是用myeclipise反向工程自动生成的。一直报错 (已经折磨我一个星期了 大神快来拯救我)
HTTP Status 500 - Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection

type Exception report

message Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:518)
.........
domain.BookDAO$$EnhancerBySpringCGLIB$$6268cd23.save(<generated>)
service.ServiceImpl.save(ServiceImpl.java:21)
action.SaveBookAction.execute(SaveBookAction.java:37)


我的struts.xml文件代码:

<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="book" namespace="" extends="struts-default">
<action name="login" class="saveBook">
<result name="success">/jsp/success.jsp</result>
</action>
</package>
</struts>


applicationContext.xml代码:

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">


<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/book"></property>
<property name="username" value="root"></property>
<property name="password" value="263015"></property>
<property name="initialSize" value="3"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>domain/Book.hbm.xml</value></list>
</property></bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="BookDAO" class="domain.BookDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="Serviceimpl" class="service.ServiceImpl">
<property name="bookDAO" ref="BookDAO"></property>
</bean>
<bean id="saveBook" class="action.SaveBookAction">
<property name="funService" ref="Serviceimpl"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>


action的代码:

public class SaveBookAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private Book book;
private ServiceImpl funService;

public Book getBook() {
return book;
}

public void setBook(Book book) {
this.book = book;
}

public FunService getFunService() {
return funService;
}

public void setFunService(ServiceImpl funService) {
this.funService = funService;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("****************");
this.funService.save(book);
return SUCCESS;
}
}



...全文
74 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Robert_110 2015-04-21
看下是不是缺少MySQL-Connector-java.jar
  • 打赏
  • 举报
回复
u010162389 2015-04-15
来人啊啊
  • 打赏
  • 举报
回复
u010162389 2015-04-14
没有人吗?快来人啊
  • 打赏
  • 举报
回复
u010162389 2015-04-14
自动生成的DAO 和pojo

public class Book implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String username;
	private String password;
	private Integer age;

	// Constructors

	/** default constructor */
	public Book() {
	}

	/** minimal constructor */
	public Book(String username, Integer age) {
		this.username = username;
		this.age = age;
	}

	/** full constructor */
	public Book(String username, String password, Integer age) {
		this.username = username;
		this.password = password;
		this.age = age;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return this.age;
	}

	public void setAge(String age) {
		this.age = Integer.parseInt(age);
	    System.out.println(this.age);
	}

}

package domain;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for Book
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see domain.Book
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class BookDAO extends  HibernateDaoSupport{
	private static final Logger log = LoggerFactory.getLogger(BookDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String AGE = "age";

	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Book transientInstance) {
		//System.out.println("-------------");
		//Transaction tr = currentSession().beginTransaction();
		//Transaction transaction= getCurrentSession().beginTransaction(); 
		log.debug("saving Book instance");
		try {
			getCurrentSession().save(transientInstance);
			//getHibernateTemplate().save(transientInstance);
			//tr.commit(); 
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Book persistentInstance) {
		log.debug("deleting Book instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Book findById(java.lang.Integer id) {
		log.debug("getting Book instance with id: " + id);
		try {
			Book instance = (Book) getCurrentSession().get("domain.Book", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Book instance) {
		log.debug("finding Book instance by example");
		try {
			List results = getCurrentSession().createCriteria("domain.Book")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Book instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Book as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByAge(Object age) {
		return findByProperty(AGE, age);
	}

	public List findAll() {
		log.debug("finding all Book instances");
		try {
			String queryString = "from Book";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Book merge(Book detachedInstance) {
		log.debug("merging Book instance");
		try {
			Book result = (Book) getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Book instance) {
		log.debug("attaching dirty Book instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Book instance) {
		log.debug("attaching clean Book instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}
然后是自己变得service接口和类:
package service;

import domain.Book;

public interface FunService {
	public void save(Book b);
}


package service;


import domain.Book;
import domain.BookDAO;

public class ServiceImpl implements FunService{
	private BookDAO bookDAO;

	public BookDAO getBookDAO() {
		return bookDAO;
	}

	public void setBookDAO(BookDAO bookDAO) {
		this.bookDAO = bookDAO;
	}
	@Override
	public void save(Book b) {
		// TODO Auto-generated method stub
		//System.out.println("----------------");
		this.bookDAO.save(b);
		}
}

  • 打赏
  • 举报
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2015-04-14 01:15
社区公告
暂无公告