static void query(String name) { Session s = null; try { s = HibernateUtils.getSession(); String hql = "from User as user where user.name=? "; // from Object; Query query = s.createQuery(hql); query.setString(0, name); List<User> list = query.list(); // executeQuery(); User u = (User) query.uniqueResult(); System.out.println(u.getName()); for (User user : list) { System.out.println(user.getName()); } } finally { if (s != null) { s.close(); } } }
static void addUser(User user) { Session s = null; Transaction tx = null; try { s = HibernateUtils.getSession(); Name name = new Name(); name.setFirstName("first name"); name.setLastName("last name"); User u = new User(); u.setName(name); tx = s.beginTransaction(); s.save(u); tx.commit(); } finally { if (s != null) { s.close(); } } }