GWT RPC Hibernate:将对象列表转换为对象数组时出错

时间:2011-12-10 23:41:23

标签: hibernate gwt rpc

我正在eclipse中使用GWT,Hibernate,RPC开发一个简单的应用程序。我试图在客户端页面上显示数据库的结果。在服务接口的实现中,我将以对象列表形式的结果转换为对象数组,但是我收到错误。这是我的代码&错误:

服务接口:

import java.util.ArrayList;

import hibDomain.User;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("testService")
public interface RpctestService extends RemoteService {

    public String addUser(String firstName,String lastName) throws IllegalArgumentException;
    public  ArrayList<User> getUser();
}
----------------

package rpctest.client;

import java.util.ArrayList;

import hibDomain.User;
import com.google.gwt.user.client.rpc.AsyncCallback;

public interface RpctestServiceAsync {

    void addUser(String firstName, String lastName,
            AsyncCallback<String> callback);

    void getUser(AsyncCallback<ArrayList<User>> asyncCallback);

}

实现:

package rpctest.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
//import com.hib.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;
import hibDomain.User;
import rpctest.client.RpctestService;

public class RpctestServiceImpl extends RemoteServiceServlet  implements RpctestService {

        public String addUser(String name1, String name2)
            throws IllegalArgumentException {

              Transaction trns = null;
              Session session = HibernateUtil.getSessionFactory().openSession();
              try {
               trns = session.beginTransaction();

               User user = new User();

               user.setFirstName(name1);
               user.setLastName(name2);

               session.save(user);

               session.getTransaction().commit();
              } catch (RuntimeException e) {
               if(trns != null){
                trns.rollback();
               }
               e.printStackTrace();
              } finally{
               session.flush();
               session.close();
              }

        return name1+name2; // to test flextable entris only 
    }

    //@Override
    public  ArrayList<User> getUser()
             {

              List<User> getUser = null;

              Transaction trns = null;
              Session session = HibernateUtil.getSessionFactory().openSession();
              try {
               trns = session.beginTransaction();
               getUser = session.createQuery("from User").list();

               //* for (Iterator<User> iter = getUser.iterator(); iter.hasNext();) 
              //{
                //User user = iter.next();
                //
              //*}             

               trns.commit();
              } catch (RuntimeException e) {
               if(trns != null){
                trns.rollback();
               }
               e.printStackTrace();
              } finally{
               session.flush();
               session.close();
             }
              return getUser.toArray(new User[getUser.size()]);
        }
}

Eclipse高亮返回getUser.toArray(新用户[getUser.size()]); &amp;建议Change method return type to User[]

1 个答案:

答案 0 :(得分:0)

方法声明将 ArrayList 称为返回类型,而您返回 User[] ,因此Eclipse是正确的,如果您单击该符号,Eclipse将建议一些更正,如果您这样做,会工作!!