请教大虾们

benqend 2007-12-29
我用jpa+spring+jsf做的一个查询(工具RAD)
但查询的时候查询不出来
下面是我的源码(省略部分import和包以及接口配置):
Entity:
import javax.persistence.*;

@Entity
@Table(name="user",schema="db2admin")
public class User {
private int id;
private String name;
private String password;

public User() {
}

public User(int id) {
this.id = id;
}

public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}

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

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

……

persisitence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="EHTMSPU"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>cn.wiztek.domain.User</class>
    <properties>
      <property name="openjpa.ConnectionURL" value="jdbc:db2://localhost:50000/testdb"/>
      <property name="openjpa.ConnectionDriverName" value="com.ibm.db2.jcc.DB2Driver"/>
      <property name="openjpa.ConnectionUserName" value="db2admin"/>
      <property name="openjpa.ConnectionPassword" value="db2admin"/>
    </properties>
  </persistence-unit>
 
</persistence>

Dao类:

public class UserDaoImpl implements UserDAO {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("EHTMSPU");
EntityManager em=emf.createEntityManager();
public List getUserByName(String name) {
Query query=em.createQuery("select u from User u where u.name=?1");
query.setParameter(1, name);
//query.setParameter(arg0, arg1, arg2)
List list=(List)query.getResultList();
return list;
}
}

Service类:

public class UserManagerImpl implements UserManager {
private final Log log=LogFactory.getLog(getClass());
ApplicationContext ctx=new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
private UserDAO userDAO=(UserDAO)ctx.getBean("userDAO");

public UserManagerImpl() {
}
public List getUserByName(String name) {
log.info("getUserByName(service)--name="+name);
return userDAO.getUserByName(name);
// TODO 自动生成方法存根
//return null;
}
}

applicationContext.xml

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="EHTMSPU" />
</bean>

    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
            ref="entityManagerFactory" />
    </bean>
<tx:annotation-driven/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
   <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="userDAO" class="cn.wiztek.dao.impl.UserDaoImpl">
</bean>

Managedbean类:

public class UserBean1 {
private String name;
private List userList;
    public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


public List getUserList(){
UserManager um=new UserManagerImpl();
userList=um.getUserByName(name);
return userList;
}
}

faces-config.xml


<managed-bean>
<managed-bean-name>User</managed-bean-name>
<managed-bean-class>cn.wiztek.domain.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>UserBean1</managed-bean-name>
<managed-bean-class>cn.wiztek.bean.UserBean1</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/page/getuserbyname.jsp</from-view-id>
<navigation-case>
<from-outcome>getbyname</from-outcome>
<to-view-id>/page/getuserbyname.jsp</to-view-id>
</navigation-case>
</navigation-rule>
……

Jsf页:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- jsf:pagecode language="java" location="/src/pagecode/page/Getuserbyname.java" --%><%-- /jsf:pagecode --%>
<%@page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<html>
<head>
<link rel="stylesheet" href="../theme/Master.css" type="text/css">
<title>getuserbyname</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<meta name="GENERATOR" content="Rational Application Developer">
<link rel="stylesheet" type="text/css" href="../theme/stylesheet.css"
title="Style">
</head>
<f:view>
<body>
<hx:scriptCollector id="scriptCollector1">
<p>Get User By Name</p>
<hr>

<h:form id="form1" styleClass="form">请输入要查询的姓名:
<h:inputText id="text1" styleClass="inputText" required="true" value="#{UserBean1.name}"></h:inputText>
<h:message id="message1" styleClass="message" for="text1"></h:message>
<br>
<hx:commandExButton type="submit" value="查询" id="button1"
styleClass="commandExButton" action="getbyname"></hx:commandExButton>
<br>
<hx:dataTableEx border="1" cellpadding="2" cellspacing="0"
columnClasses="columnClass1" headerClass="headerClass"
footerClass="footerClass" rowClasses="rowClass1, rowClass2"
id="tableEx1" styleClass="dataTable" value="#{UserBean1.userList}"
var="varuserList">
<hx:columnEx id="column2">
<f:facet name="header">
<h:outputText id="text4" styleClass="outputText" value="ID"></h:outputText>
</f:facet>
<h:outputText value="#{varuserByName.id}"></h:outputText>
</hx:columnEx>
<hx:columnEx id="column1">
<f:facet name="header">
<h:outputText id="text3" styleClass="outputText" value="Name"></h:outputText>
</f:facet>
<h:outputText value="#{varuserByName.name}"></h:outputText>
</hx:columnEx>
<hx:columnEx id="columnEx1">
<f:facet name="header">
<h:outputText id="text2" styleClass="outputText" value="Password"></h:outputText>
</f:facet>
<h:outputText value="#{varuserByName.password}"></h:outputText>
</hx:columnEx>
</hx:dataTableEx>
<hr></h:form>
</hx:scriptCollector>
</body>
</f:view>
</html>

帮我看下--------感激不尽
Global site tag (gtag.js) - Google Analytics