hibernate中用findByExample方法出現Unknown entity: XXX異常

在使用多條件查詢的時候:

public List<?> findByExample(Apply instance) {
    	System.out.println("finding Apply instance by example");
    	try {
    		List<?> results = getSession()
    		.createCriteria("Apply")
    		.add(Example.create(instance))
    		.list();
    		System.out.println("find by example successful, result size: " + results.size());
    		return results;
    		
    	} catch (RuntimeException re) {
    		log.error("find by example failed", re);
    		throw re;
    	}
    } 

在上面的代碼中,實例Apply若org.hibernate.MappingException: Unknown entity: Apply

則要引入對應的包。如下:

List<?> results = getSession()
                    .createCriteria("com.entity.Apply")
                    .add(Example.create(instance))
            .list();

同樣,利用ID查詢的時候:

public Apply findById( java.lang.Integer id) {
        System.out.println("getting Apply instance with id: " + id);
        try {
            Apply instance = (Apply) getSession()
                    .get("com.entity.Apply", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章