Note that there are some explanatory texts on larger screens.

plurals
  1. POGWT - Hibernate: How to display results from MySQL into GWT client
    primarykey
    data
    text
    <p>I am very starter to GWT and Hibernate. I made a simple GWT RPC application that adds a user to MySQL using Hibernate. I declared a single method in service interface i.e. addUser that adds a user(i.e. firstName &amp; LastName) to MySQL calling Hibernate method. Its is working fine. Now added a 2nd method to retrieve users from DB &amp; display. </p> <p>Here are service interfaces</p> <p><strong>service interfaces</strong></p> <pre><code>package rpctest.client; 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 llegalArgumentException; public ArrayList&lt;User&gt; 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&lt;String&gt; callback); void getUser(AsyncCallback&lt;ArrayList&lt;User&gt;&gt; asyncCallback); } </code></pre> <p><strong>Here is entry point class</strong></p> <pre><code>package rpctest.client; import java.util.ArrayList; import hibDomain.User; import rpctest.shared.FieldVerifier; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyPressHandler; /** * Entry point classes define &lt;code&gt;onModuleLoad()&lt;/code&gt;. */ public class Rpctest implements EntryPoint { final TextBox firstName = new TextBox(); final TextBox lastName = new TextBox(); final Button ans = new Button("Add User"); //final Label label1 = new Label("First Name"); //final Label label2 = new Label("Last Name"); private FlexTable userFlexTable = new FlexTable(); //final Label errorLabel = new Label(); private VerticalPanel mainpanel = new VerticalPanel(); private HorizontalPanel addpanel1 = new HorizontalPanel(); private HorizontalPanel addpanel2 = new HorizontalPanel(); private final RpctestServiceAsync calNumbers = GWT .create(RpctestService.class); /** * This is the entry point method. */ public void onModuleLoad() { userFlexTable.setText(0, 0, "User ID"); userFlexTable.setText(0, 1, "First Name"); userFlexTable.setText(0, 2, "Second Name"); userFlexTable.setText(0, 3, "Remove"); //add input boxes to panel addpanel1.add(firstName); addpanel1.add(lastName); firstName.setFocus(true); //add input mainpanel.add(userFlexTable); mainpanel.add(addpanel1); addpanel1.add(ans); ans.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { addStock(); } }); lastName.addKeyPressHandler(new KeyPressHandler() { public void onKeyPress(KeyPressEvent event) { if (event.getCharCode() == KeyCodes.KEY_ENTER) { addStock(); } } }); RootPanel.get().add(mainpanel); } private void addStock(){ String name1 = firstName.getValue(); // Stock code must be between 1 and 10 chars that are numbers, letters, or dots. /*if (!name1.matches("^[0-9A-Z\\.]{1,10}$")) { Window.alert("'" + name1 + "' is not a valid name."); firstName.selectAll(); return; }*/ firstName.setValue(""); String name2 = lastName.getValue(); /*if (!name2.matches("^[0-9A-Z\\.]{1,10}$")) { Window.alert("'" + name1 + "' is not a valid name."); lastName.selectAll(); return; }*/ lastName.setValue(""); firstName.setFocus(true); calNumbers.addUser(name1,name2, new AsyncCallback&lt;String&gt;() { public void onFailure(Throwable caught) { // Show the RPC error message to the user Window.alert("check your inputs"); } @Override public void onSuccess(String result) { // TODO Auto-generated method stub // Add the user to the table. int row = userFlexTable.getRowCount(); userFlexTable.setText(row, 1, result); } }); calNumbers.getUser(new AsyncCallback&lt; ArrayList&lt;User&gt;&gt;() { public void onFailure(Throwable caught) { // Show the RPC error message to the user Window.alert("Problem in database connection"); } @Override public void onSuccess( ArrayList&lt;User&gt; result) { // TODO Auto-generated method stub } }); } } </code></pre> <p><strong>Here is service implementation</strong></p> <pre><code>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&lt;User&gt; getUser() { List&lt;User&gt; getUser = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); getUser = session.createQuery("from User").list(); /* for (Iterator&lt;User&gt; iter = users.iterator(); iter.hasNext();) { User user = iter.next(); User[] arrOfObjects = new User[]{user}; } */ trns.commit(); } catch (RuntimeException e) { if(trns != null){ trns.rollback(); } e.printStackTrace(); } finally{ session.flush(); session.close(); } return (ArrayList&lt;User&gt;) getUser; } } </code></pre> <p>The getUser method in service implementation class is showing an error, highlighting method return type i.e. ArrayList But eclipse is giving no suggestion. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload