Previously I posted a easy hibernate mapping.
Here is the example manager class for this and a testcase main
The manager java class:
package hibernate.databaseTables;
import hibernate.HibernateSessionFactory;
import java.sql.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class UserManager {
private Logger log;
public UserManager(){
log = Logger.getLogger(this.getClass());
}
public UserValue createUser(UserValue user) {
log.info("creating User");
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
Integer id = (Integer) session.save(user);
user.setuserid(id);
tx.commit();
HibernateSessionFactory.closeSession();
return user;
}
public void updateUser(UserValue user) {
log.info("updating User");
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
UserValue dbuser = (UserValue) session.get(UserValue.class, user.getuserid());
if (user != null) {
dbuser.setusername(user.getusername());
dbuser.setpassword(user.getpassword());
dbuser.setsurname(user.getsurname());
dbuser.setfirstname(user.getfirstname());
dbuser.setsex(user.getsex());
dbuser.setaddress(user.getaddress());
dbuser.setzipcode(user.getzipcode());
dbuser.setcity(user.getcity());
dbuser.setemail(user.getemail());
dbuser.setphone(user.getphone());
dbuser.setmobile(user.getmobile());
dbuser.setdateofbirth(user.getdateofbirth());
}
session.flush();
tx.commit();
HibernateSessionFactory.closeSession();
}
public void listUser() {
log.info("list User");
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
List UsersList = session.createQuery("from UserValue").list();
for (Iterator iter = UsersList.iterator(); iter.hasNext();) {
UserValue user = (UserValue) iter.next();
System.out.println("Id " + user.getuserid() + " Name " + user.getusername());
}
tx.commit();
HibernateSessionFactory.closeSession();
}
public void tc_UserManager(){
// testcase to create a user to the DB
UserValue user=new UserValue();
user.setaddress("tiensestraat");
user.setcity("zichem");
user.setdateofbirth(new Date(0));
user.setemail("nico.beylemans@gmail.com");
user.setfirstname("nico");
user.setmobile("0478/540878");
user.setpassword("eenwachtwoord");
user.setphone("geen");
user.setsex(false);
user.setsurname("beylemans");
user.setusername("beyle");
user.setzipcode(3271);
try{
user=this.createUser(user);
this.listUser();
} catch (HibernateException e) {
// problem with Hibernate happened
e.printStackTrace();
}
System.out.println("primary key is " + user.getuserid());
}
}
The main testcase class:
package hibernate;
import hibernate.databaseTables.ApplicationManager;
import hibernate.databaseTables.PermissionManager;
import hibernate.databaseTables.RoleManager;
import hibernate.databaseTables.TariffTypeManager;
import hibernate.databaseTables.UserManager;
import hibernate.databaseTables.UserRoleManager;
public class TestAsApplication {
public static void main(String[] args) {
// Testcases
//testing UserManager OK
UserManager usermanager=new UserManager();
usermanager.tc_UserManager();
}
}
No comments:
Post a Comment