8:00 – 6:00 VTX
- No MIT video today. Went out and saw Star Wars. Fun! Need to see it again when the crowds thin out in an IMAX theater.
- Copied some stunt data into the hibernate_test db.
- Ran the code that set up the session and connected to the (empty) db. No exceptions, so I think it’s working this time…
- IDE is tracking annotations. The names in the annotation class need to be the same as the table and element names or there is an error
- Ok, reading and writing into the db. Now to clean it up and separate elements;
- Here’s the current cleaned up version. Still need to create the table more properly.
package com.philfeldman.mains; import com.philfeldman.mappings.Employee; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.metadata.ClassMetadata; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import java.util.Map; import java.util.Random; /** * Created by philip.feldman on 12/21/2015. * * A simple test program that will read and write from a table in a database. In MySql, the * table is in the form: CREATE TABLE employee ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(50) DEFAULT NULL, PRIMARY KEY (id) ) ; */ public class EmployeeTest { private SessionFactory sessionFactory; private ServiceRegistry serviceRegistry; private Session session; private Random rand; public EmployeeTest()throws ExceptionInInitializerError{ this.rand = new Random(); try { Configuration config = new Configuration(); config.configure("hibernate.cfg.xml"); this.serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); this.sessionFactory = config.buildSessionFactory(serviceRegistry); this.session = this.sessionFactory.openSession(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public void closeSession(){ this.session.close(); } public void printAllEntityNames(){ System.out.println("querying all the managed entities..."); final Map metadataMap = this.session.getSessionFactory().getAllClassMetadata(); System.out.println("There are [" + metadataMap.keySet().size() + "] members in the set"); for (Object key : metadataMap.keySet()) { System.out.println("key = ["+key.toString()+"]"); } } public void printAllEmployees(){ String key = Employee.class.getCanonicalName(); final Map metadataMap = this.session.getSessionFactory().getAllClassMetadata(); final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key); final String entityName = classMetadata.getEntityName(); final Query query = session.createQuery("from " + entityName); System.out.println("executing: " + query.getQueryString()); for (Object o : query.list()) { Employee e = (Employee) o; System.out.println(" " + e.toString()); } } public void addRandomEmployee(){ try { session.beginTransaction(); Employee employee = new Employee(); employee.setName("rand(" + this.rand.nextInt(100) + ")"); session.save(employee); session.getTransaction().commit(); }catch (HibernateException e){ session.getTransaction().rollback(); } } public static void main(String[] args){ try { //System.out.println("Employee.class.getCanonicalName: "+Employee.class.getCanonicalName()); /***/ EmployeeTest et = new EmployeeTest(); et.printAllEntityNames(); et.printAllEmployees(); et.addRandomEmployee(); et.closeSession(); /***/ }catch (Exception e){ e.printStackTrace(); } } }