/** * @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(); }
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 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); }
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); }
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; }
/** * @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 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(); }
/** * 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 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(); }
@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; }
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; }
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 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; }
@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 int getSearchCount(String searchQuery, String module, String baseName, String locale) { List<String> values = new ArrayList<String>(); List<NullableType> types = new ArrayList<NullableType>(); StringBuffer queryString = new StringBuffer(""); try { if (searchQuery != null && searchQuery.length() > 0) { queryString.append("(defaultValue like ? OR value like ? OR propertyName = ?)"); values.add("%" + searchQuery + "%"); values.add("%" + searchQuery + "%"); values.add(searchQuery); types.add(Hibernate.STRING); types.add(Hibernate.STRING); types.add(Hibernate.STRING); } if (module != null && module.length() > 0) { if (queryString.length() > 0) { queryString.append(" AND "); } queryString.append("moduleName = ? "); values.add(module); types.add(Hibernate.STRING); } if (baseName != null && baseName.length() > 0) { if (queryString.length() > 0) { queryString.append(" AND "); } queryString.append("baseName = ?"); values.add(baseName); types.add(Hibernate.STRING); } if (locale != null && locale.length() > 0) { if (queryString.length() > 0) { queryString.append(" AND "); } queryString.append("locale = ?"); values.add(locale); types.add(Hibernate.STRING); } if (queryString.length() > 0) { queryString.insert(0, "select count(*) from MessageBundleProperty where "); } else { queryString.insert(0, "select count(*) from MessageBundleProperty"); } Integer count = null; try { Query query = getSession().createQuery(queryString.toString()); query.setParameters(values.toArray(), (Type[]) types.toArray(new Type[types.size()])); count = (Integer) query.uniqueResult(); } catch (HibernateException e) { throw new RuntimeException(e.getMessage(), e); } return count.intValue(); } catch (Exception e) { logger.error("problem searching the message bundle data", e); } return 0; }