List of usage examples for org.hibernate Session persist
void persist(Object object);
From source file:generatehibernate.Test.java
public static void main(String[] args) { Configuration cfg = new Configuration().configure("/resources/hibernate.cfg.xml"); cfg.setInterceptor(new UserInterceptor()); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); User user = new User(); user.setUserName("Test interceptor"); user.setFullName("azza hamdy"); user.setDateOfBirth(new Date()); user.setAddress("suez"); user.setPhone("123456789"); user.setEmail("test@mail.com"); user.setRegistrationDate(new Date()); user.setPassword("4815162342"); user.setMobile("01025012646"); session.save(user);/*from www.j a v a2s. c om*/ session.persist(user); session.getTransaction().commit(); session.close(); System.out.println("Insertion Done"); }
From source file:geocity.GeoCity.java
/** * @param args the command line arguments *//* ww w. j ava 2 s. com*/ public static void main(String[] args) { //creating configuration object Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file //creating seession factory object ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) .build(); SessionFactory factory = cfg.buildSessionFactory(serviceRegistry); //creating session object Session session = factory.openSession(); Transaction t = session.beginTransaction(); City c1 = new City(); c1.setId(3); c1.setLat(5); c1.setLon(4); c1.setCode("abc"); c1.setCountryname("pakistan"); c1.setRegion(5); session.persist(c1);//persisting the object t.commit();//transaction is commited try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("GeoLiteCity-Location.csv"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println(strLine); //creating transaction object session.persist(c1);//persisting the object t.commit();//transaction is commited } //Close the input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } session.close(); }
From source file:government.data.CitizenManager.java
@Override public void addCitizen(Citizen citizen, Session session) { session.beginTransaction();/*from w ww . j a v a 2 s .c om*/ session.persist(citizen); session.getTransaction().commit(); }
From source file:government.data.ProvinceManager.java
@Override public void addProvince(Province province, Session session) { session.beginTransaction();/*from ww w . ja v a2s . c o m*/ session.persist(province); session.getTransaction().commit(); }
From source file:GTD.DL.DLDAO.DAOGeneric.java
@Override public void create(T entity) { Session session = null; Transaction tx = null;/* w w w .j ava2 s .c o m*/ try { session = this.openSession(); tx = session.beginTransaction(); session.persist(entity); tx.commit(); } catch (HibernateException e) { handleException(e, tx); } finally { if (session != null) { session.close(); } } }
From source file:herudi.implement.configSessionFactory.java
public void create(Object o) { Session session = this.sf.openSession(); Transaction tx = session.beginTransaction(); try {//w w w.j a v a2 s. c o m session.persist(o); } catch (HibernateException e) { System.out.println("Gagal Create"); } tx.commit(); session.close(); }
From source file:hibernate.DaoUser.java
public boolean presist(User u) { Session session = sessionFactory.openSession(); session.beginTransaction();/*from w ww .j a v a 2s .c om*/ //find by example User userSearched = findByMobile(session, u.getMobile()); if (userSearched == null) { session.persist(u); session.getTransaction().commit(); session.flush(); session.close(); return true; } else { session.getTransaction().commit(); session.flush(); session.close(); return false; } }
From source file:hibernate.DaoUser.java
public boolean checkUser(User u) { Session session = sessionFactory.openSession(); session.beginTransaction();//from ww w. j a v a2 s . com //find by example User userSearched = findByMobile(session, u.getMobile()); if (userSearched == null) { session.persist(u); session.getTransaction().commit(); session.flush(); session.close(); return false; } else { session.getTransaction().commit(); session.flush(); session.close(); return false; } }
From source file:hibernate5.Start.java
/** * @param args the command line arguments *//*from w ww. j av a2 s .c om*/ public static void main(String[] args) { // TODO code application logic here Libro libro1 = new Libro("El lobo estepario"); Libro libro2 = new Libro("10000 leguas de viaje submarino"); Persona yo = new Persona("Rodrigo", libro1); yo.addLibro(libro2); yo.addLibro("El Eternauta"); Libro libro3 = new Libro("Storm Tactics"); Libro libro4 = new Libro("Yo Robot"); List<Libro> nuevosLibros = new ArrayList<Libro>(); nuevosLibros.add(libro3); nuevosLibros.add(libro4); yo.addLibro(nuevosLibros); //lo guardo en la base Session sesion = HibernateUtil.getSessionFactory().openSession(); sesion.beginTransaction(); sesion.persist(yo); sesion.getTransaction().commit(); sesion.close(); //ahora agrego otros libros y otra persona Persona lu = new Persona("Lujan", "Eugenesia"); lu.addLibro("Cachorros"); sesion = HibernateUtil.getSessionFactory().openSession(); sesion.beginTransaction(); sesion.persist(lu); sesion.getTransaction().commit(); sesion.close(); //me borro sesion = HibernateUtil.getSessionFactory().openSession(); sesion.beginTransaction(); sesion.delete(yo); sesion.getTransaction().commit(); sesion.close(); System.exit(0); }
From source file:hibernate_demo.Hibernate_Demo.java
/** * @param args the command line arguments *//*from ww w.j a v a 2 s. c o m*/ public static void main(String[] args) { // get session factory object and open a session Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); //create new student object Student x = new Student(); StudentAcademic x_academic = new StudentAcademic(); x.setFirstName("Avadhoot"); x.setLastName("Athalye"); x_academic.setRollNo("S160001"); x_academic.setStudent(x); x.setAcademic(x_academic); Address permanent = new Address(); permanent.setAddressLine1("Neelkanth Building, A / 201"); permanent.setAddressLine2("Ghodbandar Road, Near Railway Colony"); permanent.setCity("Thane"); permanent.setPincode("400612"); permanent.setState("Maharashtra"); permanent.setCountry("India"); Address correspondence = new Address(); correspondence.setAddressLine1("Neela Apartment, B / 23"); correspondence.setAddressLine2("Dabolkar Road, Near ganesh tower"); correspondence.setCity("Malad"); correspondence.setPincode("410612"); correspondence.setState("Maharashtra"); correspondence.setCountry("India"); x.setCorrespondence(correspondence); x.setPermanent(permanent); session.persist(x); //Couple of books instance Books b = new Books(); b.setTitle("Biochemistry 101"); //setting the student object b.setStudent(x); Books c = new Books(); c.setTitle("Mathematics: Intermidiate"); c.setStudent(x); //for bidirectional assign the books to student x.getBooks().add(b); x.getBooks().add(c); //create instance of course Course java = new Course(); java.setTitle("Core java programming"); x.getCourses().add(java); Course cpp = new Course(); cpp.setTitle("C++ Programming"); x.getCourses().add(cpp); //student is persisted here session.persist(java); session.persist(cpp); session.persist(x); //Working with inheritance Lecturer lecturer = new Lecturer(); lecturer.setName("Anonymous"); Regular Avadhoot = new Regular(); Avadhoot.setName("Avadhoot Athalye"); Avadhoot.setSalary(40000); Visiting Jay = new Visiting(); Jay.setName("Jay Teachar"); Jay.setHourlyCharges(1000); session.persist(lecturer); session.persist(Avadhoot); session.persist(Jay); tx.commit(); session.close(); }