Hibernate vs. iBatis ( updated )

Hibernate and iBatis are both Database Persistence Frameworks.

Here’s the difference of both, and which to choose for appropriate usage.

Hibernate – Hibernate is seen as larger and has more functionality, and seems to be more advanced in Cache. It offers a close to Java oriented coding where there is less bother on SQL. Downside of large frameworks is the learning curve to learn something new aside from the SQL.

Hibernate workflow:

1.) Database connectivity is already encapsulated, just edit a config file and add the Database location/URL , username and password.

2.) There is a need to create an xml file and POJO ( with getters and setters ) that maps to a database table. Only one xml file and one POJO for each corresponding table.

3.) Create a Java Code, then use the hibernate methods to access the database. In a hibernate funtion, you may use SQL , HQL or none, as it depends on the method used.

Example of HQL java code:
> http://examples.javacodegeeks.com/enterprise-java/hibernate/hibernate-query-language-example/
> http://www.mkyong.com/hibernate/hibernate-query-examples-hql/

MyBatis – ( previously called iBatis ) is much more simplified and lightweight compared to Hibernate. Also best to use if there is an already existing live ( complex ) database, since it has the ability to map it easily because of its simple approach. iBatis still uses SQL and does not encapsulate it unlike Hibernate.

MyBatis workflow:

1.) Database connectivity is already encapsulated, just edit a config file and add the Database location/URL , username and password, which is quite similar to Hibernate.

2.) Create a DTO which is a POJO that similary corresponds to a database table or a structured dataset.

3.) All SQL commands and queries are written in the xml mapper file. Then each entry of a SQL set command is mapped to the DTO java file. There can be multiple mapper files and DTO files, as needed.

4.) To use the SQL commands, use the DTO java file and use the methods with the corresponding parameters as described in the xml mapper file.

Example of Mapper files.
> https://mybatis.github.io/mybatis-3/sqlmap-xml.html
> http://examples.javacodegeeks.com/enterprise-java/mybatis/how-create-mybatis-mapper/

Summary:

For a more Java and Object Oriented approach, Hibernate provides this. If comfortable to use SQL, and less on Java coding, iBatis can do it.

See more from this site:

http://www.tutorialspoint.com/ibatis/ibatis_vs_hibernate.htm

Struts + Spring + Hibernate Integration

Posted on 2014/07/20, Updated on 2015/08/30

Update: For new software development or building a new website from ground-up, there is better design alternative instead of using the Struts framework. Using JSTL is a better alternative, and here is a post in Webpage development using JSTL, Javascript, and JQuery. For older webpage systems, created before 2014, the Java and Struts combination is popular, and therefore a lot of legacy systems runs using these technologies.

Let’s Remember these important files that are needed in configuration of a Java Web application.
( Which includes Spring, Hibernate, and Struts )

web.xml – Servlet class to URL mapping
applicationContext.xml – Spring mapping of Bean/Object injection
struts.xml – maps the defined action to the servlet ( action class is needed in struts ) to the result jsp
hibernate.cfg.xml– database connection such as JDBC name , user/password to access the database name is defined here
<classname>.hbm.xml – the database table hibernate mapping file

index.jsp – the initial action URL, the startpage

Steps:

1.) Define org.springframework.web.context.ContextLoaderListener as the listener and org.apache.struts2.dispatcher.FilterDispatcher as the filter in web.xml

2.) Basically Spring is used here to create the predefined values, as these can be done by using the bean property injection in applicationContext.xml

Here’s a detailed explanation at this site: http://www.tutorialspoint.com/struts_2/struts_spring.htm

Website Login Security : Comparison of Java and PHP

Login and authentication is the basic and important function in almost all websites. I will just give a summary of my research and how login and authentication is done in JAVA and PHP.

JAVA

By using Spring Securities and Spring MVC jars ( by using Maven or downloading the jar files from the internet ), Website login can be easily created by following tutorial instructions. The basic tutorial with authentication can be created in just one day ( according to my experience ). SQL authentication is not yet included from the basic, but can be easily added. Knowledge of Spring MVC and Servlet Filters are also needed to understand the concept.

Spring Security tutorials by Mkyong
http://www.mkyong.com/tutorials/spring-security-tutorials/

The basic Spring Security example
http://www.mkyong.com/spring-security/spring-security-hello-world-example/

PHP

I also researched on how to this in PHP, and saw results with core PHP and another using Laravel. PHP is known to have very unstable and messy code ( which is why maintainability is an issue ). But with Laravel Framework, PHP code is improved.

PHP hardcode using MYSQL database for secure login
http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

PHP authentication using Laravel
http://laravel.com/docs/5.0/authentication

Laravel features discussed including login and authentication
http://code.tutsplus.com/tutorials/why-laravel-is-taking-the-php-community-by-storm–pre-

PHP and JAVA codes are very different in syntax and implementation, and both languages has its advantage and disadvantage.

Top Server-Side languages for Websites

The basics of software are machine instructions that are sent to the computer processor so that the computer can perform computations, store data, and display media and information.

On this post, Server-side website software languages are discussed. Therefore, software for embedded software ( in machines and robotics ) is not included in the category. Client-side and front end scripts used for screen display such as CSS, HTML and Javascript is excluded. Software for mobile hardware such as Android and iOS are also excluded. Only the languages that control server-side logic is on the list. This is different in my previous post on Best Programming languages to learn, which covers all programming languages in general.

Another note is that some websites use more than one server-side language, maybe for transition ( such as Ameba.jp , a Japanese blog site ), or which uses only one ( Facebook which uses only PHP, or LinkedIn, which only uses Java )

Below is the basic list on where this area is under in the Website architecture.

[O] Front-end code -> CSS, HTML, JS

[/] Back-end code -> PHP, Java, C#, Ruby

[O] Database -> Oracle, MySQL, MongoDB

[O] OS -> Linux, Solaris, Windows

A very important thing to consider on the selection of a Server-Side language is Performance, Quick setup and development, Maintenance, and the most important factor is the Availability and talent pool of developers.

 

Top Server-Side languages for Websites

1.) PHP

PHP is on the top of the list. according to w3techs.com, PHP powers 81% of all internet websites ( as of July 2015 ). All other languages do not come close. According to this report, PHP is used by most medium traffic sites, where these sites composes 80% of the internet. PHP is highly used in Social Media sites ( ex. Facebook and etc. ) Blogs ( ex. WordPress ) and Wiki sites. Theses sites are the internet platform used by most people.

PHP is described to be “the quickest web development language to learn and setup” and is preferred by most freelance and start-up developers. It is open source, with crude and simple scripting.

Here’s the official PHP page: https://secure.php.net/

2.) Java

Java programming language is popular among e-commerce and corporate websites, although it only comes to third with only 3% of all internet traffic ( ASP.net takes second place ). but according to this report, Java is in a steady rise, while PHP is on a steady decline. Although both languages have the same capabilities, Java may win on the performance since its compiled code, unlike PHP or Ruby.

Java is also open source and is integrated with J2EE technology and other 3rd party software such as Hibernate and Spring to become a full-tier webserver.

Recently, J2EE has become too large and complicated, with much maitenance overhead. Even the Struts framework has become outdated. There’s a new Play framework ( derived from Scala ) which eliminates all the complications of the J2EE architecture, but this is yet a rising technology and still on its semi-experimental stage.

On personal opinion, I am closely following Play framework, and just recently, Java creator James Gosling is already a consultant for Typesafe Inc. , the creator of Scala and Play framework ( according to Wikipedia.com ).

3.) C# and .NET

This comes as the second most-popular server-side programming language at 16.7% ( As of July 2015 ). C# and ASP.net are created and maintained from single vendor which is Microsoft Inc. The ASP.net comes as one package for Website development. C# is still in demand with several websites that are tied-up with this technology. A single vendor has an advantage over integration and a single contact point, but it offers less flexibility and vendor lock dependency, which most software firms avoid.

On the bright side, C# basic code has a very similar syntax to Java, so the switch has a lesser learning curve.

some links on C# and ASP.net tutorials

http://asp.net-tutorials.com/

http://asp.net-tutorials.com/basics/code-behind/

4.) Ruby on Rails / Node.js / Perl and other languages

RoR / Ruby on Rails rails was popular since it was designed to create a website as quickly as possible. That’s good for developers, and startups, but it’s bad news for a business or an enterprise using it as a long term and high traffic site, since performance was an issue. Node.js has risen in popularity, since knowledge of JavaScript is all that is required to code ( where JavaScript is the most widely used client-side scripting ), No need to code on Java and JavaScript or PHP and JavaScript on single Web project. But there are also credits to Ruby, where other frameworks ( even Play framework ) is getting its influence. Other languages such as Perl, Scala and Phyton combined takes only 1% of the total usage and popularity.

Websites and links that show why Ruby is abandoned.

http://stackoverflow.com/questions/3794270/is-ruby-on-rails-or-at-least-the-community-dying

http://ilikekillnerds.com/2015/02/is-ruby-on-rails-dying/

 

Play framework – The next web server phenomenon

As I have learned, there are more advancements in Web technologies which makes software development much easier, less complicated, and its end product to be highly efficient, stable, robust, fast, reliable and yet takes complex requirements. And top of it all, it should never take the user experience for granted, and keep in mind the developer experience as well.

Play framework is an emerging technology. It is a web server that can be created in Java or Scala. ( Scala is not as popular as Java, but it was created to address Java shortcomings ) It is foreseen to replace the J2EE web server technologies. It is comparable to MongoDB ( or any NoSQL ) , which has very high potential of replacing the SQL.

To code using Play framework, the concept is in MVC but without servlets or any web server, since Play is already the web server. Other MVC frameworks will not be used also ( ex. Spring ). Front end code is still needed such as Javascript ( or CoffeScript, JQuery, AngularJS ) and CSS. Server side logic can be coded using Java or Scala.

Comparison with J2EE: J2EE web technologies have been the standard and reliable web server on most enterprise applications, with Java as the top programming language of choice. J2EE technology is built on top or constructed with other modules to make it functional. A full stack J2EE web application should have a J2EE container / web server, the J2EE application package itself, which is integrated with a MVC framework such as Spring and a Database access component such as Hibernate. Currenty J2EE application have become bloated with layers of modules and log files have too much hierarchy. It is because J2EE has become an increasing version of add-ons and enhancements.

Play framework eliminates all of those J2EE modules. It is not a Servlet technology. Play framework is a Non-Blocking I/O. Non-Blocking I/O means that HTTP request and processing in CPU side of the server is asynchronous, Yet Servlet 3.1 already supports Non-Blocking I/O, Play framework has Websocket support, which is not in Servlet 3.1. And best part of Play framework is the simplicity of the debug logs and application modules, where J2EE has its drawbacks.

I’m really impressed with this new Web Server advancements in Play framework. It is really comparable to the shift from SQL to NoSQL. SQL and NoSQL databases have the same purpose, a Database, yet its design and architecture is completely different. For simplicity, yet have served its function as a Database, NoSQL ( ex. MongoDB ) does it. It is much easier to setup ( ex. in Java, we just need the libraries and codes for transaction ), compared to SQL ( ex. Hiberante ) , which needs code, models, configuration files, and even integration with Spring to make its code less. So in this case, Play framework is like the NoSQL solution to an already legacy code such as J2EE.

Some Cons: Yes Play is better, but it is still new. So there may be some uncertainties hiding in the code. Another drawback of Play is the Scala and its build code ( called SBT , which is similar to Ant / Maven ). Play is built in combination with Java and Scala, So for non-Scala developers, it’s a steep learning curve.

Production components: For production and deployment, Play is integrated with Netty ( HTTP server ) and Akka ( Background Process ). No need of Webservers ( Tomcat, JBoss, IBM Websphere and etc.. ) . Other sub components are Templates, SBT and Websockets ( Websockets and be used as an option only ). Just include front end JS and CSS to be added in Play. Other third party libraries in Java ( if using Play as Java based, not Scala based ) can be added.

Bonus Factors: This technology is really new, with only a few enterprise websites using it, and as of this writing, they only have 5 on their list. See the list here in their home page. LinkedIn is one popular site using Play, so there is no question of its performance. I also checked zaptravel.com , and it loads pretty fast too. Compared to MongoDB which already has numerous users, across all industries, so similarly, in a matter of years, Play framework will get more attention.

Highlights:

– Lightweight
– Has authentication and security
– Can easily be switched from development mode to production mode via a single configuration.
– Has support for Design Patterns ( ex. Singleton and etc .. )
– Not a Servlet
– Not tied up to SQL . Can handle other Database types also
– No need of Web Server ( Or a servlet container ). The Play framework is the server itself.
– Can be scaled for production
– pretty darn straight forward, not so layered.
– Supports Unit test
– Better error detection / Direct to the point compile errors, does not display a full stack trace
– built as non blocking I/O. Adapts to multi core CPU.
– Much better compared to node.js and ruby on rails. Which I still have to find out the reason…

Here’s the explanation why Play framework is really fast.
https://playingscala.wordpress.com/2015/05/01/why-play-framework-is-fast/

Some important links:

1.) routing of http requests:

http://stackoverflow.com/questions/15907996/how-to-get-query-string-parameters-in-java-play-framework?rq=1

2.) basics

https://www.playframework.com/documentation/1.2.x/faq

3. ) philosophy

https://www.playframework.com/documentation/2.3.x/Philosophy

4.) One user even got embarrassed.. or how it is unbelievalbe good.

https://dzone.com/articles/play-framework-now-officially

Mongo DB in Windows: Recovery after improper shutdown

Simple Tip:
When you encounter a problem when Mongo DB will not start up,
Go to the Data folder ( see the data folder here ) and delete the LOCK file. Then restart the MongoDB and check the results. If it still fails, delete all files in the data folder. Yes, but only do this on a Development or Test system.

To properly shutdown MongoDB, use the “Ctrl+C” command in the Windows console where MongoDB is running.

Mongo DB in Java: Get a single database entry Value by Key

How to get a single value String by using the key of a single return from a Database search query?

As an example, we would get each value in a single row. But we first have to search the collection ( or in SQL terms, a table ) where the email address is equal to some value. In this case we use the [email] variable

See the sample Java code below, Then each line is commented with an explanation


//This will store the single row result in a setter-getter object
Account newAccount = new Account();

//create the "where" query to search an email.
//"email" first parameter is the key. [email] second parameter is //the search string, which is a variable.
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("email", email);

//find the collection row, using the where condition created.
DBCursor cursor = accountsColl.find(whereQuery);

//loop the result using a DB cursor
while(cursor.hasNext()) {

 //store the "cursor.next()" value in another object,
 //since directly accessing the result will cause 
 //a NoElementException when this type of loop is used
 DBObject tobj = cursor.next();

 //Also store the Strings in a separate objects
 String tempName = (String)tobj.get("name");
 String tempEmail = (String)tobj.get("email");
 String tempPass = (String)tobj.get("password");

 //set the results to the Account Object
 newAccount.setName(tempName);
 newAccount.setEmail(tempEmail);
 newAccount.setPassword(tempPass);

}

return newAccount;