java - How should I design the classes for a simple CRUD operation in JSF + Hibernate application? -


i trying learn web programming using jsf , hibernate.

i have table called mytable in mysql server has 2 columns: name, surname

i have mytable.java mapped table, looks this:

// listing 1 public class mytable {      private string name;     private string surname;      public mytable() {     }      public string getname() {          return name;     }      public void setname(string name) {         this.name = name;     }      public string getsurname() {         return surname;     }      public void setsurname(string surname) {         this.surname = surname;     } } 

this mapped via hibernate, code ( have used testing purposes ) inserts data mytable:

    //listing 2     session session = hibernateutil.getsessionfactory().opensession();      session.begintransaction();      mytable mytable = new mytable();      mytable.setname("name");     mytable.setsurname("surname");      session.save(mytable);      session.gettransaction().commit(); 

and form on index.xhtml looks like:

// listing 3 <h:form id="registirationform" prependid="false">      <h:outputlabel for="name">name:</h:outputlabel>     <h:inputtext id="name"></h:inputtext>      <h:outputlabel for="surname">surname:</h:outputlabel>     <h:inputtext id="surname"></h:inputtext>  </h:form> 

my question is:

how tie form code above?

  • should make class mytable managed bean, , put method called saverecord , put above code in there?
  • should mapped other class? if should called? should change name of current mytable.java mytabledao.java ?

so when user enters name, , surname, method should called?

where should put code found in listing 2?

i hope question clear, helping.

you have basic model-view-controller pattern here. mytable model , index.html view. tie them i'd suggest adding controller class registrationcontroller.

the controller managed , view fields using its' properties this:

<h:outputlabel for="name">name:</h:outputlabel> <h:inputtext id="name" value="#{registrationcontroller.username}"></h:inputtext> 

the controller contain save method. may add dao object suggested separate persistence details buisness logic (which should in controller).

so when user enters name , surname values, these set properties on controller (which may directly translated mytable object). when save clicked, save method called on controller uses 'dao' persist 'mytable' db.

you may want this tutorial.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -