@Override public List<Forum> getForumsOfaUser(int userId) { // TODO Auto-generated method stub List<Forum> forums = new ArrayList<Forum>(); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery("from Subscription where userId = :userId"); query.setString("userId", String.valueOf(userId)); List<Subscription> userSubscribedGroups = query.list(); for (Subscription subsc : userSubscribedGroups) { query = session.createQuery("from Forum where forumId = :forumId"); query.setParameter("forumId", subsc.getForumId()); // add this to a list variable query.uniqueResult(); forums.add((Forum) query.uniqueResult()); } } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return forums; }
public List<Team> getTeams() { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); List result = null; try { Transaction transaction = session.beginTransaction(); result = session.createQuery("from Team").list(); session.flush(); transaction.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally { if (session.isOpen()) { session.close(); } } List<Team> teams = new ArrayList<Team>(); for (Object o : result) { if (o instanceof Team) { teams.add((Team) o); } } return teams; }
public EventNumberOfPassesRequest getEventPassesRequest(User user, Event event) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from EventNumberOfPassesRequest as eventNumberOfPassesRequest " + "where eventNumberOfPassesRequest.idevent=" + event.getIdevent() + " and eventNumberOfPassesRequest.iduser=" + user.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { return (EventNumberOfPassesRequest) result.get(0); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return null; }
public void savePartnerRequest(ActivityPartnerRequest activityPartnerRequest) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from ActivityPartnerRequest as partnerrequest " + "where partnerrequest.idactivity=" + activityPartnerRequest.getIdactivity() + " and partnerrequest.iduser=" + activityPartnerRequest.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { Session session1 = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction1 = session1.beginTransaction(); for (Object o : result) { session1.delete(o); } transaction1.commit(); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } saveObject(activityPartnerRequest); }
/** * If a domain class uses more than one datasource, we need to know which one to use when calling * a method without a namespace qualifier. * * @param domainClass the domain class * @return the default datasource name */ public static String getDefaultDataSource(PersistentEntity domainClass) { List<String> names = getDatasourceNames(domainClass); if (names.size() == 1 && Mapping.ALL_DATA_SOURCES.equals(names.get(0))) { return Mapping.DEFAULT_DATA_SOURCE; } return names.get(0); }
public List<Activity> getActivities(Event parent) { List<Activity> result = new ArrayList<Activity>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object event = session .createQuery("from Event as event where event.idevent= " + parent.getIdevent()) .uniqueResult(); transaction.commit(); if (event != null) { Event eventEntity = (Event) event; Collection<Activity> activityList = eventEntity.getActivities(); for (Activity activity : activityList) { result.add(activity); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public static void main(String[] args) { EventManager mgr = new EventManager(); if (args[0].equals("store")) { mgr.createAndStoreEvent("My Event", new Date()); } else if (args[0].equals("list")) { List events = mgr.listEvents(); for (int i = 0; i < events.size(); i++) { Event theEvent = (Event) events.get(i); System.out.println("Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()); } } else if (args[0].equals("addpersontoevent")) { Long eventId = mgr.createAndStoreEvent("My Event", new Date()); Long personId = mgr.createAndStorePerson("Foo", "Bar"); mgr.addPersonToEvent(personId, eventId); System.out.println("Added person " + personId + " to event " + eventId); } else if (args[0].equals("addemailtoperson")) { Long personId = mgr.createAndStorePerson("Foozy", "Beary"); mgr.addEmailToPerson(personId, "foo@bar"); mgr.addEmailToPerson(personId, "bar@foo"); System.out.println( "Added two email addresses (value typed objects) to person entity : " + personId); } HibernateUtil.getSessionFactory().close(); }
/** * @param session the hibernate session * @return a string listing the different subscribers present in the database */ @SuppressWarnings("unchecked") public String toString(Session session) { List<Media> medias = (List<Media>) session.createQuery("from Media").list(); String s = "List of medias (" + medias.size() + ") present in the library:\n"; for (Media m : medias) s += "\t" + m.toString() + "\n"; return s; }
public ActivityPartnerRequest getPartnerRequest(User user, Activity activity) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from ActivityPartnerRequest as partnerrequest " + "where partnerrequest.idactivity=" + activity.getIdactivity() + " and partnerrequest.iduser=" + user.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { return (ActivityPartnerRequest) result.get(0); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return null; }
public void saveEventPassesRequest(EventNumberOfPassesRequest eventNumberOfPassesRequest) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from EventNumberOfPassesRequest as eventNumberOfPassesRequest " + "where eventNumberOfPassesRequest.idevent=" + eventNumberOfPassesRequest.getIdevent() + " and eventNumberOfPassesRequest.iduser=" + eventNumberOfPassesRequest.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { Session session1 = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction1 = session1.beginTransaction(); for (Object o : result) { session1.delete(o); } transaction1.commit(); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } saveObject(eventNumberOfPassesRequest); }
@SuppressWarnings("unchecked") public List<String> getAllBaseNames() { List<String> retValue = getHibernateTemplate() .find("select distinct(baseName) from MessageBundleProperty order by baseName"); if (retValue == null) return new ArrayList(); // force deep load retValue.size(); return retValue; }
public User getUserByEmail(String email) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); List list = session.createQuery("from User as user where user.mail = '" + email + "'").list(); transaction.commit(); if (list != null && list.size() == 1) { return (User) list.get(0); } return null; }
/** * @param session the hibernate session * @return a string in HTML listing the different subscribers present in the database */ @SuppressWarnings("unchecked") public String toHTML(Session session) { List<Media> medias = (List<Media>) session.createQuery("from Media").list(); if (medias.size() <= 0) return "No media present in the library."; StringBuilder sb = new StringBuilder(); sb.append("<p>List of medias (" + medias.size() + ") present in the library:</p>"); sb.append(Media.toHTML(medias)); return sb.toString(); }
private User getUserByEmployeeId(String employeeId) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); String queryString = "from User user where user.employeeId ='" + employeeId + "'"; List list = session.createQuery(queryString).list(); transaction.commit(); if (list != null && list.size() == 1) { return (User) list.get(0); } return null; }
/** step through queue and call the real saveOrUpdateInternal method to do the work */ public void run() { List queueList = new ArrayList(queue); for (Iterator i = queueList.iterator(); i.hasNext(); ) { SaveOrUpdateCall call = (SaveOrUpdateCall) i.next(); try { saveOrUpdateInternal( call.baseName, call.moduleName, call.bundleData, call.loc.toString()); } catch (Throwable e) { logger.error("problem saving bundle data:", e); } finally { queue.remove(call); } } }
public MessageBundleProperty getProperty(MessageBundleProperty mbp) { Object[] values = new Object[] { mbp.getBaseName(), mbp.getModuleName(), mbp.getPropertyName(), mbp.getLocale() }; String sql = "from MessageBundleProperty where baseName = ? and moduleName = ? and propertyName = ? and locale = ?"; List<?> results = getHibernateTemplate().find(sql, values); if (results.size() == 0) { logger.debug("can't find a default value for : " + mbp); return null; } return (MessageBundleProperty) results.get(0); }
public List<ActivityPartnerRequest> getAllActivityPartnerRequestForUser(User user) { List<ActivityPartnerRequest> result = new ArrayList<ActivityPartnerRequest>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); List list = session .createQuery("from ActivityPartnerRequest apr where apr.iduser=" + user.getIduser()) .list(); transaction.commit(); if (list != null && list.size() > 0) { for (Object o : list) { result.add((ActivityPartnerRequest) o); } } return result; }
public User getUserByName(String firstName, String lastName) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); String queryString = "from User user where user.firstname ='" + firstName + "' and user.lastname ='" + lastName + "'"; // System.out.println("queryString = " + queryString); List list = session.createQuery(queryString).list(); transaction.commit(); if (list != null && list.size() == 1) { return (User) list.get(0); } return null; }
public void runHibernate() { // ############################################################################ Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Message message = new Message("Hello World"); session.save(message); tx.commit(); session.close(); // ############################################################################ Session secondSession = HibernateUtil.getSessionFactory().openSession(); Transaction secondTransaction = secondSession.beginTransaction(); List messages = secondSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } secondTransaction.commit(); secondSession.close(); // ############################################################################ Session thirdSession = HibernateUtil.getSessionFactory().openSession(); Transaction thirdTransaction = thirdSession.beginTransaction(); // message.getId() holds the identifier value of the first message Message loadedMessage = (Message) thirdSession.get(Message.class, message.getId()); loadedMessage.setText("Greetings Earthling"); loadedMessage.setNextMessage(new Message("Take me to your leader (please)")); thirdTransaction.commit(); thirdSession.close(); // ############################################################################ // Final unit of work (just repeat the query) // TODO: You can move this query into the thirdSession before the // commit, makes more sense! Session fourthSession = HibernateUtil.getSessionFactory().openSession(); Transaction fourthTransaction = fourthSession.beginTransaction(); messages = fourthSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } fourthTransaction.commit(); fourthSession.close(); // Shutting down the application HibernateUtil.shutdown(); }
public Map<String, String> getBundle(String baseName, String moduleName, Locale loc) { Object[] values = new Object[] {baseName, moduleName, loc.toString()}; String sql = "from MessageBundleProperty where baseName = ? and moduleName = ? and locale = ? and value != null"; List<?> results = getHibernateTemplate().find(sql, values); Map<String, String> map = new HashMap<String, String>(); if (results.size() == 0) { logger.debug( "can't find any overridden values for: " + getIndexKeyName(baseName, moduleName, loc.toString())); return map; } for (Iterator<?> i = results.iterator(); i.hasNext(); ) { MessageBundleProperty mbp = (MessageBundleProperty) i.next(); map.put(mbp.getPropertyName(), mbp.getValue()); } return map; }
public List<Event> getEvent(String name) { List<Event> result = new ArrayList<Event>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List list = session.createQuery("from Event as event where event.name='" + name + "'").list(); transaction.commit(); for (Object o : list) { result.add((Event) o); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return result; }
/** * queues up a call to add or update message bundle data * * @param baseName * @param moduleName * @param bundleData * @param loc */ protected void queueBundle( String baseName, String moduleName, Map<String, String> bundleData, Locale loc) { SaveOrUpdateCall call = new SaveOrUpdateCall(); call.baseName = baseName; call.moduleName = moduleName; call.bundleData = bundleData; call.loc = loc; queue.add(call); }
public List<User> getUsers() { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); List<User> result = new ArrayList<User>(); try { Transaction transaction = session.beginTransaction(); List list = session.createQuery("from User").list(); transaction.commit(); for (Object o : list) { User user = (User) o; result.add(user); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return result; }
public List<User> getUsersForTeam(Team team) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); List list = session .createQuery( "from TeamUserSelection as teamUserSelection where teamUserSelection.idteam= " + team.getId()) .list(); transaction.commit(); if (list != null) { List<User> result = new ArrayList<User>(); for (Object o : list) { User user = getUser(((TeamUserSelection) o).getIduser()); result.add(user); } return result; } return null; }
public List<Logo> getLogos() { List<Logo> result = new ArrayList<Logo>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List list = session.createQuery("from Logo").list(); transaction.commit(); for (Object o : list) { Logo logo = (Logo) o; result.add(logo); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return result; }
public List<Activity> getActivities() { List<Activity> result = new ArrayList<Activity>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List list = session.createQuery("from Activity").list(); transaction.commit(); for (Object o : list) { Activity activity = (Activity) o; result.add(activity); } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
@SuppressWarnings("unchecked") public List<PostAndReply> getPostsAndReplies(int forumId) { // TODO Auto-generated method stub Session session = sessionFactory.openSession(); Transaction tx = null; List<Post> postArr = new ArrayList<Post>(); List<PostAndReply> prArray = new ArrayList<PostAndReply>(); try { tx = session.beginTransaction(); Query query = session.createQuery("from Post where forumId = :forumId order by createdDate desc"); query.setParameter("forumId", forumId); postArr = query.list(); System.out.println("No of posts.." + postArr.size()); for (Post post : postArr) { int postId = post.getPostId(); System.out.println("Post id----" + postId); List<Reply> replies = getRepliesToPost(postId); User user = getUser(post.getCreatedBy()); String postCreatorName = user.getFirstname() + ' ' + user.getLastname(); PostAndReply pr = new PostAndReply(); pr.setPost(post); pr.setReplies(replies); pr.setPostCreatorName(postCreatorName); prArray.add(pr); // System.out.println("Post - Description----"+post.getDescription()); // System.out.println("No of Replies----"+replies.size()); } } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return prArray; }
public List<Location> getLocations() { List<Location> result = new ArrayList<Location>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List list = session.createQuery("from Location").list(); transaction.commit(); if (list != null) { for (Object o : list) { Location location = (Location) o; result.add(location); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public List<ClassificationType> getClassificationTypes() { List<ClassificationType> result = new ArrayList<ClassificationType>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List list = session.createQuery("from ClassificationType").list(); transaction.commit(); if (list != null) { for (Object o : list) { ClassificationType classificationType = (ClassificationType) o; result.add(classificationType); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public List<User> getUserForActivity(Activity activity) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); List<User> result = new ArrayList<User>(); try { Transaction transaction = session.beginTransaction(); String queryString = "select user from User as user where " + activity.getIdactivity() + " in elements (user.activities)"; List users = session.createQuery(queryString).list(); transaction.commit(); for (Object user : users) { result.add((User) user); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return result; }