This essay developed out of conversations I've had with several other programmers about why Java smelled suspicious. It's not a critique of Java!
Sycorax - complete tutorials
Programming Tutorials
" Java provides the industry - software companies and customer alike , an opportunity to create a true open computing environment where software is portable,
and customers benefit from increase competition. "
Java and the Future
December 1, 2008-LEJB 3.1: EJB New and Improved!
The EJB 3.0 specification was a huge improvement from what you were used to in the early versions of EJB. Available as an early draft, EJB 3.1 has many more features and is even easier to use.
December 1, 2008-Should Java Assert that Network I/O Can't Occur on the UI Thread?
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it's still done.
Register now to recieve special alert and latest technology news!
Session
Before writing the business component,we need to understand a hibernate 'session'.A hibernate session is an instance of org.hibernate.SessionopenSession() of SessionFactory. Every database operation begins and ends with a Session. For example,we can beginTransaction(),save(),update() or delete() an object with the help of a Session. and is obtained by calling
Our business component will use the Session to perform its business operations.
package com.sycorax.hibernate.client;
import org.hibernate.Session;
import com.sycorax.hibernate.User;
/**
* UserManager *
@author sycorax
*/
public class UserManager {
private Session session = null;
public UserManager(Session session) {
if(session == null)
throw new RuntimeException("Invalid session object. Cannot instantiate the UserManager");
this.session = session;
}
public void saveUser(User user) {
session.save(user);
}
public void updateUser(User user) {
session.update(user);
}
public void deleteUser(User user) {
session.delete(user);
}
}
Let us write a test client that utilizes the different methods of the UserManager we have just introduced. We will obtain the SessionFactory and open a session and then call different methods of the UserManager to see if they work or not.
package com.sycorax.hibernate.client;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import com.sycorax.hibernate.*;
public class TestClient {
/* * This method builds a default user to check if the Hiernate configuration is working or not */
public User buildUser() {
User user = new User();
user.setFirstName("Gc");
user.setLastName("Das");
user.setAge(25);
user.setEmail("info@sycorax.co.in");
return user;
}
/* * This method gets the default SessionFactory configured in hibernate.cfg.xml and opens a session */
public Session openSession()
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session =sessionFactory.openSession();
return session;
}
public User testSaveUser(UserManager manager) {
User user = buildUser();
manager.saveUser(user);
System.out.println("User saved with ID = " user.getUserId());
return user;
}
public void testUpdateUser(UserManager manager,User user) { user.setFirstName("Andrew");
manager.updateUser(user);
System.out.println("User updated with ID = " user.getUserId());
}
public void testDeleteUser(UserManager manager,User user) { manager.deleteUser(user);
System.out.println("User deleted with ID = " user.getUserId());
}
public static void main(String[] args) {
TestClient client = new TestClient();
Session session = client.openSession();
UserManager manager = new UserManager(session);
User user = client.testSaveUser(manager);
client.testUpdateUser(manager,user);
client.testDeleteUser(manager,user);
session.flush();
}
}