无法使用hibernate从mySQL检索数据

时间:2017-03-02 20:16:34

标签: mysql hibernate spring-mvc netbeans

我正在为我的学校项目开发一个Web应用程序......我按照一些教程在使用hibernate的SPring mvc中创建CRUD。我无法从mySQL数据库中检索任何数据。我的结果如下面的链接..

Result (Click Here)

的hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/highereducation</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">mysql</property>        
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="format_sql">true</property>        
    <mapping resource="entity/University.hbm.xml"/>
</session-factory>

hibernate.reveng.xml中

<hibernate-reverse-engineering>
  <schema-selection match-catalog="highereducation"/>
  <table-filter match-name="university"/>
</hibernate-reverse-engineering>

University.java

public class University  implements java.io.Serializable {


 private Integer univId;
 private String univName;
 private String univAdd;
 private String univType;

public University() {
}

public University(String univName, String univAdd, String univType) {
   this.univName = univName;
   this.univAdd = univAdd;
   this.univType = univType;
}
//getter & setter below

HibernateUtil.java

public class HibernateUtil {

private static final SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
    try {            
        sessionFactory = new Configuration().configure().buildSessionFactory();

    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

universityModel.java

public class universityModel {

public List<University> getAll(){

    Session s = HibernateUtil.getSessionFactory().getCurrentSession();
    List<University> univList = new ArrayList<University>();
    try{
        s.beginTransaction();
        univList = s.createCriteria(University.class).list();
        s.getTransaction().commit();
    }catch(Exception ex){
        ex.printStackTrace();
    }

    return univList;
}
}

universityController.java

@Controller
@RequestMapping(value="/university")
public class universityController {

@RequestMapping(value="/all", method=RequestMethod.GET)
public String getAll(Model m){

    universityModel model = new universityModel();
    m.addAttribute("univList", model.getAll());

    return "allUniversity";
}

}

allUniversity.jsp

<body> 
    <table border="1" style="border-collapse: collapse" cellpadding="7px">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Address</th>
            <th>Type</th>
            <th>Action</th>
        </tr>
        <c:forEach items="${allUniv}" var="univ">
            <tr>
                <td>${univ.univId}</td>
                <td>${univ.univName}</td>
                <td>${univ.univAdd}</td>
                <td>${univ.univType}</td>
                <td>Edit | Delete</td>
            </tr>
        </c:forEach>
    </table>

</body>

请帮助......提前致谢

0 个答案:

没有答案