Hibernate Annotations This example is the same as the first example except that it uses annotations. There we first started by creating the .hbm.xml file, here there is no need to create it instead we will use annotations to do the object relational mapping.
Step 1 : Remove the patient.hbm.xml file
Step 2 ; Open the hibernate.cfg.xml and replace the following line
I am not sure whether title for this post is correct or wrong. But in the post , i am going to write how hibernate works if we play around in the application.
1. Tables are automatically created After Hibernate Introduction Part 1, i just went to the back end database, change the table name from patient
to patient1. Now i again run the application, it runs without any exception. But when i refresh the db at the back end, i saw , again patient table is created and record is inserted.
So What is hibernate ? Here is the answer Click the following link Link
:) Well, if you have time, just go thru each and understand. Otherwise, simply put, using hibernate it is each save the data into the database or load the data from the database.
Well, let's start.
Step 1: Development environment
1. My sql server 5.0 Download 2. Hibernate 4.1.1 Download 3. JDBC Driver for Mysql ((mysql-connector-java-5.1.19.zip)) Download 4. Eclipse IDE
Unzip the hibernate buddle and you should have the following structure after you unzip Documentation folder lib Folder Project folder log text file hibernate logo image license text file
Step 2:
Open mysql and create the following table in any of the exisitng database or create new database. Here is the script for our table
1. Click File -> New -> Other -> Java Project 2. Say Example1 for Project Name and leave all other to default values. (Make sure, JRE has been configured). 3. Next we will add all dependencies in the example1. Now click Example1 project in the project explorer and right click -> Click New-> Folder. Say folder name as lib
Now copy the following files into lib folder (Eclipse project explorer accepts all copy and paste operation. For example, you can copy file or folder in windows explorer and then you can come back to eclipse project explorer and paste it)
All the files in Hibernate->Lib->required. All the files in Hibernate->Lib->jpa. All the files in Hibernate->Lib->envers mysql-connector-java-5.1.18-bin.jar
4. Now setup the build path. In order to execute our example, all the above jar files should be in class build path
Again, click on the Example1, right click, and select build Path->configure build path. Go to Libraries-> External jars, browse lib folder you created earlier. Then add external jars by
selecting all jar files that you copied to lib folder in one of previous step
Step 4: Creating Java beans
Now let us create the java bean for the patient where we want to store in the database.
Again, right click on Example1, Select New -> Class; Package Name : mypack and Class name :patient Leave the all other to default values
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
}
Step 5: Mapping between Java bean and Table.
Let us first summarize what we have done so far. 1) My sql table 2) Java bean.
Now, important step, we will see how to map 1) and 2) , so that whenever new object is created, it will insert one record in the database.
Hibernate makes persisting the state of your Java objects incredibly simple. However, in order for
Hibernate to know where to story your JavaBeans, or how to map the property of a JavaBean to a database
column, the developer has to provide a bit of direction to the Hibernate framework.
This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table
in the database it has to access, and what columns in that table it should use.
Using JPA annotations also, we can do the same. In this example, first we will see using mapping files and then we will convert the same example to JPA annotations.
Now let us create the mapping file Right Click src->New->File. File Name as patient.hbm.xml
Now right click on test.java file and select Run as -> Java Application If every thing configured correctly, then you will the see the following lines in the console at the end
Now we will see how we can access all these components and do some actions. In this example, i have just changed the caption, so that's action for us now.
In order to manipulate the UI Components, let us follow the MVC Pattern
First we need to create a composer and let us connect the zul and composer by using apply attribute
@SuppressWarnings("rawtypes") public class Example5 extends GenericForwardComposer {
private static final long serialVersionUID = 1L; private Window outerWin; private Window innerWin; private Button innerBtn; private Button outerBtn;
@SuppressWarnings("unchecked") public void doAfterCompose(Component comp) throws Exception { /** * Dont forget to call super doaftercomposer. By calling this, it will * add the event listner for us */ super.doAfterCompose(comp);
outerWin = (Window) comp; // Set the outer window title outerWin.setTitle("Title Changed");
// Now let us try to access outerbtn by different methods
/** * The Path provides the utility method getComponent which takes the * relative path of the component as its argument. outerBtn is * equivalent to outerWin/outerBtn */
/** * The getFirstChild method returns the first child component of the * caller. The advantage of using this method is that you don't even * need to know the component ID to fetch the component. */
Finally , we will end using zk 6 An Annotation Based Composer For MVC This is the 5th post in a series of ZK MVC Here is the zk documentation and we are following that and creating new example http://books.zkoss.org/wiki/Small_Talks/2008/August/ZK_MVC_Made_Easy http://books.zkoss.org/wiki/Small_Talks/2011/January/Envisage_ZK_6:_An_Annotation_Based_Composer_For_MVC
Here is the Summary
Method 1 : Implements Composer We implement Composer interface. Here we used getfellow method to hold the reference for the UI Components and also we added listener to handle events
Here is the Link for the first post http://emrpms.blogspot.in/2012/04/mvc-using-composer-interface.html
Method 2 : extends GenericComposer Here, we removed all the event lisener by extending the GenericComposer But remember, we need to call super doaftercompose.
Here is the link for the second post http://emrpms.blogspot.in/2012/04/zk-mvc-using-genericcomposer-utility.html
Method 3: extends GenericAutowireComposer Here, we removed all the getfellow methods and let them auto wired Here we dont need override doaftercompose
Method 4: extends GenericForwardComposer Here, we removed forward attributes using GenericForwardComposer utility class Here is the link http://emrpms.blogspot.in/2012/04/zk-mvc-using-genericforwardcomposer.html
Finally we will the same output using zk 6 An Annotation Based Composer For MVC
A composer analogous to GenericForwardComposer. Instead of wiring variables and adding event listeners by naming convention, this composer do the work by annotation and selectors.
Here is my zul code
<?page title="Example10" contentType="text/html;charset=UTF-8"?> <zk> <label value="http://books.zkoss.org/wiki/Small_Talks/2008/August/ZK_MVC_Made_Easy. http://books.zkoss.org/wiki/Small_Talks/2011/January/Envisage_ZK_6:_An_Annotation_Based_Composer_For_MVC" style="font-size : 18px;font-family: verdana,arial,sans-serif;" /> <separator /> <window title="MVC Pattern An Annotation Based Composer For MVC" border="normal" width="700px" apply="com.me.Example10"> <grid> <columns> <column label="" /> <column label="" /> </columns> <rows> <row> First Name : <textbox id="firstName" /> </row> <row> Last Name : <textbox id="lastName" /> </row> <row> Address : <textbox id="address" /> </row> <row> <button id="Clear" label="Clear" /> </row>