import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.apache.log4j.Logger;
public class Runner {
final static Logger logger = Logger.getLogger(Runner.class);
private List
public Runner() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Runner aRunner = new Runner();
aRunner.addNewUsers();
aRunner.showAllUsers();
aRunner.addSharedPhoneNumber();
}
/*
* show how to add records to the database
*/
private void addNewUsers() {
Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
/*
* all database interactions in Hibernate are required to be inside a transaction.
*/
Transaction transaction = session.beginTransaction();
/*
* create some User Java objects.
*/
User aUser = new User();
aUser.setUname("Sandman");
aUser.setPword("aPass");
User anotherUser = new User();
anotherUser.setUname("jordan");
anotherUser.setPword("packham");
/*
* save each object as a record in the database
*/
session.save(aUser);
session.save(anotherUser);
transaction.commit();
/*
* prove that the user objects were added to the database and that
* the objects were updated with the database generated user id.
*/
System.out.println("aUser generated ID is: " + aUser.getId());
System.out.println("anotherUser generated ID is: " + anotherUser.getId());
}
/*
* show how to get a collection of type List containing all of the records in the app_user table
*/
private void showAllUsers() {
Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
/*
* execute a HQL query against the database.
*/
users = session.createQuery("select u from User as u order by u.id").list();
System.out.println("num users: "+users.size());
/*
* iterate over each User object returned by the query
*/
Iterator
while(iter.hasNext()) {
User element = iter.next();
System.out.println(element.toString());
System.out.println("num of phone numbers: "+element.getPhoneNumbers().size());
}
transaction.commit();
}
/*
* show how to modify a database record
*/
private void modifyUser() {
Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
/*
* get a single User object form the database. The second object in the users list could be used here instead but I
* wanted you to see how to get back just one object from the database.
*/
User anotherUser = (User)session.createQuery("select u from User as u where u.uname='jordan'").uniqueResult();
/*
* change the user name for the Java object
*/
anotherUser.setUname("Joshua");
/*
* call the session merge method for the User object in question. This updates the database table.
*/
session.merge(anotherUser);
transaction.commit();
/*
* prove that the database was updated by printing out all of the User objects created by a HQL query
*/
showAllUsers();
}
private void addSharedPhoneNumber() {
Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
/*
* get a single User object form the database. The second object in the users list could be used here instead but I
* wanted you to see how to get back just one object from the database.
*/
User firstUser = (User)session.createQuery("select u from User as u where u.uname='Joshua'").uniqueResult();
/*
* change the user name for the Java object
*/
Set
PhoneNumber anotherNumber = new PhoneNumber();
anotherNumber.setPhone("(546)222-9898");
session.save(anotherNumber);
phoneNumbers.add(anotherNumber);
/*
* call the session merge method for the User object in question. This updates the database table.
*/
session.merge(firstUser);
User secondUser = (User)session.createQuery("select u from User as u where u.uname='aName'").uniqueResult();
/*
* set the single phone number to be used by more than one User
*/
phoneNumbers = secondUser.getPhoneNumbers();
phoneNumbers.add(anotherNumber);
/*
* call the session merge method for the User object in question. This updates the database table.
*/
session.merge(secondUser);
transaction.commit();
/*
* prove that the database was updated by printing out all of the User objects created by a HQL query
*/
showAllUsers();
}
private void deleteAddedUsers() {
// TODO Auto-generated method stub
Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
int numUsers = users.size();
for(int i = 2; i < numUsers; i++){
System.out.println("deleting user "+users.get(i).getUname());
User aUser = users.get(i);
session.delete(users.get(i));
}
transaction.commit();
/*
* at this point the records have been removed from the database but still exist in our class list attribute.
* Do not store retrieved lists since they will be out of synch with the database table from which they come.
* This example shows that you should not store retrieved lists.
*/
System.out.println(users);
users.remove(2);
users.remove(2);
/*
* now the Java objects are also gone.
*/
System.out.println(users);
}
}
No comments:
Post a Comment