List of usage examples for org.hibernate Session persist
void persist(Object object);
From source file:cs544.blog.dao.UserDAOHibernate.java
@Override public void saveUser(User user) { Session session = sessionFactory.getCurrentSession(); session.persist(user); }
From source file:cs544.blog.dao.UserDAOHibernate.java
@Override public void updateUser(User user) { Session session = sessionFactory.getCurrentSession(); // session.update(user); User userExisted = loadUser(user.getId()); // Session session = sessionFactory.getCurrentSession(); if (userExisted != null) { session.delete(userExisted);/*w w w . j ava 2 s . c om*/ session.persist(user); } //saveUser(user); }
From source file:cs544.exercise02_1.AppBook.java
public static void main(String[] args) { // Hibernate placeholders Session session = null; Transaction tx = null;/*ww w .ja va 2s . c om*/ try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // Create new instance of Book and set values in it Book book1 = new Book("Spring in Action", "8574524578451", "Rob Jhonshon", 54.21, new Date()); // save the book1 session.persist(book1); // Create new instance of Book2 and set values in it Book book2 = new Book("Expert in Spring", "3434524578451", "Habibur Rahman", 74.21, new Date()); // save the book1 session.persist(book2); // Create new instance of Book3 and set values in it Book book3 = new Book("Spring in Spring", "533524578451", "Amit Jhonshon", 44.21, new Date()); // save the book1 session.persist(book3); tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") List<Book> bookList = session.createQuery("from Book").list(); for (Book book : bookList) { System.out.println( "name= " + book.getTitle() + ", ISBN= " + book.getISBN() + ", author= " + book.getAuthor()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve one book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") Book book = (Book) session.get(Book.class, 1); System.out.println( "name= " + book.getTitle() + ", ISBN= " + book.getISBN() + ", author= " + book.getAuthor()); //set new author name for book id 1 book.setAuthor("Masudur Rahman"); //update new book attribute session.update(book); //retrive one book again and will delete Book bookInstance = (Book) session.load(Book.class, 3); //remove data from database session.delete(bookInstance); tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") List<Book> bookList = session.createQuery("from Book").list(); for (Book book : bookList) { System.out.println( "name= " + book.getTitle() + ", ISBN= " + book.getISBN() + ", author= " + book.getAuthor()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } // Close the SessionFactory (not mandatory) sessionFactory.close(); System.exit(0); }
From source file:cs544.exercise03_1.AppOwner.java
public static void main(String[] args) { // Hibernate placeholders Session session = null; Transaction tx = null;/*from ww w . ja v a 2 s . c om*/ try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // Create new instance of Object and set values in it Owner owner = new Owner("Masudur Rahman", "Fairfield, IA"); session.persist(owner); Owner owner2 = new Owner("Fahim Muntasir", "Dhaka, BD"); session.persist(owner2); tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") List<Owner> ownerList = session.createQuery("from Owner").list(); for (Owner owner : ownerList) { System.out.println("name= " + owner.getName() + ", address= " + owner.getAddress()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } }
From source file:cs544.exercise04_1.AppEmployee.java
public static void main(String[] args) { // Hibernate placeholders Session session = null; Transaction tx = null;// w ww . ja va2s . com try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // Create new instance of Object and set values in it Laptop laptop1 = new Laptop("Sonny", "VAIO"); Laptop laptop2 = new Laptop("Dell", "INSPIRON"); Set<Laptop> laptops = new HashSet<Laptop>(); laptops.add(laptop1); laptops.add(laptop2); Employee employee = new Employee("Fahim", "Muntasir"); laptop1.setEmployee(employee); laptop2.setEmployee(employee); employee.setLaptop(laptops); session.persist(employee); tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") List<Employee> employeeList = session.createQuery("from Employee").list(); for (Employee empployee : employeeList) { System.out.println( "FirstName= " + empployee.getFirstname() + ", lastName= " + empployee.getLastname()); } List<Laptop> laptopList = session.createQuery("from Laptop").list(); for (Laptop laptop : laptopList) { System.out.println("Brand= " + laptop.getBrand() + ", Type= " + laptop.getType()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } }
From source file:cs544.exercise04_1.AppPassenger.java
public static void main(String[] args) { // Hibernate placeholders Session session = null; Transaction tx = null;/*w w w . j a v a2 s . c o m*/ try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // Create new instance of Object and set values in it Flight flight1 = new Flight("FL001", new Date(), new Date(), new Date()); Flight flight2 = new Flight("FL002", new Date(), new Date(), new Date()); List<Flight> flights = new ArrayList<Flight>(); flights.add(flight1); flights.add(flight2); Passenger passenger = new Passenger("Masudur Rahman"); passenger.setFlights(flights); session.persist(passenger); tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") List<Passenger> passList = session.createQuery("from Passenger").list(); for (Passenger pass : passList) { System.out.println("Name= " + pass.getName()); } List<Flight> flList = session.createQuery("from Flight").list(); for (Flight fl : flList) { System.out.println("No= " + fl.getFlightnumber() + ", from= " + fl.getFrom() + " To= " + fl.getTo() + ", date= " + fl.getDate()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } }
From source file:cs544.exercise04_1.AppSchool.java
public static void main(String[] args) { // Hibernate placeholders Session session = null; Transaction tx = null;/*from w w w . j a v a 2 s . c o m*/ try { session = sessionFactory.openSession(); tx = session.beginTransaction(); School school = new School("Maharishi University"); // Create new instance of Object and set values in it Student st1 = new Student("Masudur", "Rahman"); Student st2 = new Student("Habibur", "Rahman"); Map<Integer, Student> studentMap = new HashMap<Integer, Student>(); studentMap.put(st1.getStudentid(), st1); studentMap.put(st2.getStudentid(), st2); school.setStudents(studentMap); session.persist(school); tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } //retrieve book list from db try { session = sessionFactory.openSession(); tx = session.beginTransaction(); // retieve all books @SuppressWarnings("unchecked") List<School> schoolList = session.createQuery("from School").list(); for (School school : schoolList) { System.out.println("Name= " + school.getName()); } List<Student> stList = session.createQuery("from Student").list(); for (Student st : stList) { System.out.println("Student First Name= " + st.getFirstname() + ", Last Name= " + st.getLastname()); } tx.commit(); } catch (HibernateException e) { if (tx != null) { System.err.println("Rolling back: " + e.getMessage()); tx.rollback(); } } finally { if (session != null) { session.close(); } } }
From source file:ctd.services.addReference.java
License:Apache License
/** * * @return message/*from www. j a va2 s . c om*/ */ public String addReference() { String message = ""; //open hibernate connection SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Query q = session.createQuery("from Ticket where password='" + getPassword() + "'"); Ticket ticket = (Ticket) q.uniqueResult(); Iterator it1 = ticket.getStudySampleAssaies().iterator(); while (it1.hasNext()) { StudySampleAssay ssa = (StudySampleAssay) it1.next(); String name = ssa.getNameRawfile(); name = name.replace(".CEL", ""); if (name.equals(getName_RAWFILE())) { ssa.setXREF(getReference()); } } Transaction tr = session.beginTransaction(); session.saveOrUpdate(ticket); session.persist(ticket); tr.commit(); session.close(); sessionFactory.close(); message = getReference() + " is added."; return message; }
From source file:ctd.services.getCleanData.java
License:Apache License
public String cleanData() throws IOException, NoImplementationException, SerializerException { CleanDataResult result = new CleanDataResult(); String message = ""; String error_message = ""; //get parameters. ResourceBundle res = ResourceBundle.getBundle("settings"); ResourceBundle cdf_list = ResourceBundle.getBundle("cdf"); //Base directory ftp folder: Here the subfolders are found for each set of CEL-files. String ftp_folder = res.getString("ws.ftp_folder"); String rscript_cleandata = res.getString("ws.rscript_cleandata"); String rscript = res.getString("ws.rscript"); //db//from w ww . j a v a 2 s .c om String db_username = res.getString("db.username"); String db_password = res.getString("db.password"); String db_database = res.getString("db.database"); //open hibernate connection SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tr = session.beginTransaction(); Query q = session.createQuery( "from Ticket where password='" + getPassword() + "' AND ctd_REF='" + getCTD_REF() + "'"); Ticket ticket = null; String closed = ""; if (q.list().size() != 0) { ticket = (Ticket) q.list().get(0); closed = ticket.getClosed(); } if (ticket == null) { error_message = "Ticket password and CTD_REF don't match."; } if (closed.equals("yes")) { error_message = "Ticket is allready used for normalization of these CEL-files."; ticket = null; } if (ticket != null) { //get the folder String folder = ticket.getFolder(); //create subfolder containing the derived zip file String zip_folder = ftp_folder + folder; //get content File dir = new File(zip_folder); String[] files = dir.list(); //find the zip file. String cel_zip_file = ""; String zip_file = ""; String gct_file = ""; for (int i = 0; i < files.length; i++) { String file = files[i]; if (file.contains("zip")) { cel_zip_file = file; zip_file = zip_folder + "/" + cel_zip_file; gct_file = zip_folder + "/gctfile_" + folder; } } Process p3 = Runtime.getRuntime().exec("chmod 777 " + zip_file); ////////////////////////////////////////////////////////////////// //Do a system call to normalize. R. (zip_folder zip_file gct_file rscript) String args = rscript + " --vanilla " + rscript_cleandata + " -i" + zip_file + " -o" + gct_file + " -w" + zip_folder; Process p = Runtime.getRuntime().exec(args); //Check if CEL files are unzipped allready boolean do_loop = true; while (do_loop) { File dir2 = new File(zip_folder); String[] files2 = dir2.list(); //Check if CEL files are allready there for (int i = 0; i < files2.length; i++) { String file = files2[i]; if (file.endsWith("chip")) { do_loop = false; try { Thread.sleep(5000); } catch (InterruptedException ex) { Logger.getLogger(getCleanData.class.getName()).log(Level.SEVERE, null, ex); } } } } File dir2 = new File(zip_folder); String[] files2 = dir2.list(); String chip_file = ""; String chip_file_db = ""; ArrayList<String> unziped_files = new ArrayList<String>(); for (int i = 0; i < files2.length; i++) { String file = files2[i]; if (file.endsWith("CEL")) { unziped_files.add(file); } if (file.endsWith("chip")) { chip_file = file; chip_file_db = chip_file.split("_CDF_")[1]; } } //Check if all CEL files are derived from the same chip. //This is essential for normalization. //initiate check hashmap. This map contains all the unique chip definition file names. There should be only one per analysis. ArrayList<StudySampleAssay> map = new ArrayList<StudySampleAssay>(); for (int i = 0; i < unziped_files.size(); i++) { String cel_file = unziped_files.get(i); StudySampleAssay ssa = new StudySampleAssay(); // Open the file that is the first // command line parameter String cel_file_path = zip_folder + "/" + cel_file; String name = cel_file.replaceAll(".CEL", ""); ssa.setNameRawfile(name); ssa.setXREF(name); map.add(ssa); } ticket.getStudySampleAssaies().addAll(map); session.saveOrUpdate(ticket); session.persist(ticket); tr.commit(); session.close(); //Storage chip definition file (CDF), creation gct file and database storage. SessionFactory sessionFactory1 = new Configuration().configure().buildSessionFactory(); Session session1 = sessionFactory1.openSession(); List<ChipAnnotation> chip_annotation = null; //check if cdf (chip definition file) is allready stored, if not, store it. Query q2 = session1.createQuery("from Chip Where Name='" + chip_file_db + "'"); if (q2.uniqueResult() != null) { Chip chip = (Chip) q2.list().get(0); chip_annotation = chip.getChipAnnotation(); } if (q2.uniqueResult() == null) { //Add this chip and its annotation Chip chip_new = new Chip(); chip_new.setName(chip_file_db); //read chip file String chip_file_path = zip_folder + "/" + chip_file; chip_annotation = readChip(chip_file_path); //Store the whole chip_new.getChipAnnotation().addAll(chip_annotation); Transaction tr1 = session1.beginTransaction(); session1.save(chip_new); session1.persist(chip_new); tr1.commit(); session1.close(); } //create the temp file for storage of the data_insert file. String data_file = zip_folder + "/expression.txt"; FileOutputStream out = null; PrintStream pr = null; out = new FileOutputStream(data_file); pr = new PrintStream(out); //create array data input file for the database table, find correct foreign keys. //get the study_sample_assay id and the probeset ids. SessionFactory sessionFactory2 = new Configuration().configure().buildSessionFactory(); Session session2 = sessionFactory2.openSession(); //Get the cip_annotation_id Query q3 = session2.createQuery("from Chip Where Name='" + chip_file_db + "'"); Chip chip = (Chip) q3.list().get(0); chip_annotation = chip.getChipAnnotation(); Iterator it2 = chip_annotation.iterator(); //for speed, put the chip annotation id in a hashmap HashMap<String, String> chip_annotation_ids = new HashMap<String, String>(); while (it2.hasNext()) { ChipAnnotation ca = (ChipAnnotation) it2.next(); String id = ca.getId().toString(); String ps = ca.getProbeset(); chip_annotation_ids.put(ps, id); } try { Query qt = session2.createQuery( "from Ticket where password='" + getPassword() + "' AND ctd_REF='" + getCTD_REF() + "'"); ticket = null; if (qt.list().size() != 0) { ticket = (Ticket) qt.list().get(0); } Iterator it3 = ticket.getStudySampleAssaies().iterator(); while (it3.hasNext()) { StudySampleAssay ssa = (StudySampleAssay) it3.next(); String name_raw_file = ssa.getNameRawfile(); String ssa_id = ssa.getId().toString(); error_message = error_message + name_raw_file; String gct_file_generated = gct_file + ".gct"; ArrayList<Double> values = writeFile(pr, chip_annotation_ids, ssa_id, gct_file_generated, name_raw_file); Statistics stat = new Statistics(); stat.setData(values); Double average = stat.getAverage(); Double std = stat.getSTD(); ssa.setXREF(name_raw_file); ssa.setAverage(average); ssa.setStd(std); } } catch (IOException e) { } pr.close(); out.close(); Transaction tr2 = session2.beginTransaction(); session2.update(ticket); session2.persist(ticket); tr2.commit(); session2.close(); String u = "--user=" + db_username; String passw = "--password=" + db_password; String[] commands = new String[] { "mysqlimport", u, passw, "--local", db_database, data_file }; Process p4 = Runtime.getRuntime().exec(commands); message = message + " RMA and GRSN on the CEL-files is done, data is stored."; //close the ticket when finished, normalization can only be performed once by the client. CloseTicket(); //remove data_file Process p5 = Runtime.getRuntime().exec("rm -f " + data_file); Process p6 = Runtime.getRuntime().exec("rm -f " + zip_folder + "/*.CEL"); } //////////////////// //SKARINGA result.setErrorMessage(error_message); result.setMessage(message); ObjectTransformer trans = null; try { trans = ObjectTransformerFactory.getInstance().getImplementation(); } catch (NoImplementationException ex) { Logger.getLogger(getTicket.class.getName()).log(Level.SEVERE, null, ex); } message = trans.serializeToString(result); return message; }
From source file:ctd.services.getCleanData.java
License:Apache License
private void CloseTicket() { //open hibernate connection SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Query q = session.createQuery( "from Ticket where password='" + getPassword() + "' AND ctd_REF='" + getCTD_REF() + "'"); Ticket ticket = null;/*from w w w.j a va 2 s . c om*/ if (q.list().size() != 0) { ticket = (Ticket) q.list().get(0); ticket.setClosed("yes"); } Transaction tr = session.beginTransaction(); session.saveOrUpdate(ticket); session.persist(ticket); tr.commit(); session.close(); }