Ejemplo n.º 1
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();
  }
 /** 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 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;
 }