1. 程式人生 > >記hibernate一次坑

記hibernate一次坑

在使用hibernate反轉工程時有一個坑放在這裡,避免大家跳進去。本人用的是myeclipse2017ci,在使用hibernate反轉工程生成原始dao方法時碰到的bug。在方法public Account findById(Long id)中有一段程式碼及其坑爹

log.debug("getting Account instance with id: " + id);
		try {
			Account instance = (Account) getSession().get("Account", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}

改為原始的session.get(***.class,id)

public Account findById(Long id) {
		log.debug("getting Account instance with id: " + id);
		try {
			Account instance = (Account) getSession().get(Account.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

不然經常報錯Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: Account並非是自己配置檔案寫錯了,而是傳入的引數不準確,導致自己查了幾天bug。