来自hibernate的primefaces数据表选择不起作用

时间:2014-12-09 04:55:09

标签: java hibernate jsf jsf-2 primefaces

所以意图是jsf + hibernate集成。所以当我们每次运行程序时,hibernate应该执行select from table name,并且这些值应该填充在primefaces数据表中

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="solodemo")
public class solodemo {
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public solodemo(String fname, String lanme) {
        this.fname = fname;
        this.lanme = lanme;
    }
    public String getLanme() {
        return lanme;
    }
    public void setLanme(String lanme) {
        this.lanme = lanme;
    }
    solodemo(){}
    @Column(name="fname")
    @Id
    private String fname;
    @Column(name="lname")
    private String lanme;



}

hibernate pojo class

import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.hibernate.Query;
import org.hibernate.Session;

import hibernate.Util;
@ManagedBean(name="datafromtable")
@RequestScoped
public class Solomain {
private ArrayList<solodemo> list;
    public ArrayList<solodemo> getList() {
    return list;
}
public void setList(ArrayList<solodemo> list) {
    this.list = list;
}
    public ArrayList<solodemo> init(){
Session sn=Util.getSessionFactory().openSession();
sn.beginTransaction();


Query qry=sn.createQuery(" from solodemo p");


 list=new ArrayList<solodemo>(qry.list());



System.out.println("yo bitch you have done it");
return  list;
    }

}

这是托管bean

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>
<p:dataTable value="#{datafromtable.list}"  var="so">

<p:column headerText="firstname">
<h:outputText value="#{so.fname}"/>

</p:column>
<p:column  headerText="lanme">

<h:outputText value="#{so.lname}"/>
</p:column>

</p:dataTable>
</h:form>
</h:body>
</html>

这是xhtml文件,每次运行此文件时都应该转到托管bean,然后选择查询应该从数据库中获取数据,它应该填充在datatable中。但是hibernate查询没有执行,所以它返回null值数据表以任何方式纠正这个问题

1 个答案:

答案 0 :(得分:-1)

我在做了以下更改后得到了结果

public ArrayList<Solodemo> init(){
Session sn=Util.getSessionFactory().openSession();
sn.beginTransaction();


Query qry=sn.createQuery("from Solodemo");


ArrayList<Solodemo> list= new ArrayList<Solodemo>(qry.list());

并在xhtml文件中

<p:dataTable value="#{data.init()}"  var="so">