多列hibernate查询到xhtml页面

时间:2014-05-01 20:04:57

标签: mysql hibernate xhtml

我正在尝试在hibernate中进行多列查询,我想知道如何将结果映射到xhtml文件(我正在使用JSF / seam)。

所以,这是我的hibernate查询:

@Name("theusers")
@AutoCreate
@Scope(SESSION)
public class Users implements Serializable {
@In
private EntityManager entityManager;

String unameit;
public String getUnameit() {
    return unameit;
}
public void setUnameit(String unameit) {
this.unameit = unameit;
}

private List<Myuser[]> theusers;

public List<Myuser[]> getUsers(String uname) {
try {

        theusers = (List<Myuser[]>)entityManager.createQuery("SELECT username, lastname from Myuser where username = :uname")
    .setParameter("uname", uname)
        .getResultList();

    } catch(Exception e) {
        theusers = null;
    }
    return theusers;
    }
}

基本上我想从表user_detail中检索用户名和lastName列。 这就是我想在xhtml的两列中可视化的方式:

<h:form id="un_form">
<p>Enter your username: <h:inputText id="unameit" value="#{theusers.unameit}" size="40"       /></p>
<p><h:commandButton value="Submit" /></p>
</h:form>

<h:dataTable value="#{theusers.getUsers(theusers.unameit)}" var="u">
<h:column>
<f:facet name="header">
User name
</f:facet>
#{u.getUserName()}
</h:column>
<h:column>
<f:facet name="header">
User last name
</f:facet>
#{u.getLastName()}
</h:column>
</h:dataTable>

最后是Myuser.java:

@Entity
@Table(name="user_detail")
@DiscriminatorColumn(name = "obj_type", discriminatorType = DiscriminatorType.STRING,   length = 255)
public abstract class Myuser { //implements Serializable {
@Id
public int id;
public String username;
public String firstname;
public String lastname;

@Column(name="id")
public int getId() {
        return id;
}

@Column(name = "username")
public String getUserName() {
        return username;
}

@Column(name = "firstName")
public String getFirstName() {
        return firstname;
}

@Column(name = "lastName")
public String getLastName() {
        return lastname;
}

public Myuser(int id, String username, String firstname, String lastname) {
    this.id = id;
    this.username = username;
    this.firstname = firstname;
    this.lastname = lastname;
}

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

public void setFirstName( String firstname ) {
        this.firstname = firstname;
}

public void setLastName( String lastname ) {
        this.lastname = lastname;
}

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

我一直收到错误。可能有什么不对?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的查询未返回“Myuser”列表,它返回用户名和姓氏列表

如果您将查询更改为...

from Myuser where username = :uname

并将返回类型更改为List<Myuser>

然后,您将返回可在数据表中使用的Myusers列表

另外你需要重新修改你的支持bean并查看一下,需要从h:commandButton调用方法getUsers,这会在你的bean中设置'theusers'

@Name("theusers")
@AutoCreate
@Scope(SESSION)
public class Users implements Serializable {
@In
private EntityManager entityManager;

String unameit;
public String getUnameit() {
    return unameit;
}
public void setUnameit(String unameit) {
this.unameit = unameit;
}

private List<Myuser> theusers;

public void getUsers() {
        try {
            theusers = (List<Myuser>)entityManager.createQuery("from Myuser where username = :uname")
            .setParameter("uname", uname)
            .getResultList();

            }catch(Exception e) {
                theusers = null;
            }
        }
}

和你的页面......

<h:form id="un_form">
<p>Enter your username: <h:inputText id="unameit" value="#{theusers.unameit}" size="40"       /></p>
<p><h:commandButton action="#{theusers.getUsers}" value="Submit" /></p>
</h:form>

<h:dataTable value="#{theusers.theusers}" var="u">
<h:column>
<f:facet name="header">
User name
</f:facet>
#{u.getUserName()}
</h:column>
<h:column>
<f:facet name="header">
User last name
</f:facet>
#{u.getLastName()}
</h:column>
</h:dataTable>