/**
   * Creates foreign keys for subclass tables that are mapped using table per subclass. Further
   * information is available in the <a href="http://jira.grails.org/browse/GRAILS-7729">JIRA
   * ticket</a>
   */
  private void createSubclassForeignKeys() {
    if (subclassForeignKeysCreated) {
      return;
    }

    for (PersistentClass persistentClass : classes.values()) {
      if (persistentClass instanceof RootClass) {
        RootClass rootClass = (RootClass) persistentClass;

        if (rootClass.hasSubclasses()) {
          Iterator subclasses = rootClass.getSubclassIterator();

          while (subclasses.hasNext()) {

            Object subclass = subclasses.next();

            // This test ensures that foreign keys will only be created for subclasses that are
            // mapped using "table per subclass"
            if (subclass instanceof JoinedSubclass) {
              JoinedSubclass joinedSubclass = (JoinedSubclass) subclass;
              joinedSubclass.createForeignKey();
            }
          }
        }
      }
    }

    subclassForeignKeysCreated = true;
  }
예제 #2
0
  public static void main(String[] args) {
    Session session = null;

    try {

      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      session = sessionFactory.openSession();

      Contact contact;

      String SQL_QUERY = "select c.id, c.firstName from Contact  c where c.id >= 3";

      Query query = session.createQuery(SQL_QUERY);
      Iterator it = query.iterate();
      while (it.hasNext()) {
        Object[] row = (Object[]) it.next();
        System.out.println("------------------------------------------------------");
        System.out.println("ID                : " + row[0]);
        System.out.println("First Name  : " + row[1]);
      }

      System.out.println("Done");
    } catch (Exception e) {
      // tx.rollback();
      System.out.println(e.getMessage());
    } finally {

      // session.flush();
      session.close();
    }
  }
 /** 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);
     }
   }
 }
예제 #4
0
  public Boolean addOrUpdateMedia(Session session, Media media) {
    try {
      // Need to save the loans before saving the media
      Map<Integer, Loan> tabEx = media.getTabExemplaries();
      for (Iterator<Loan> i = tabEx.values().iterator(); i.hasNext(); )
        session.saveOrUpdate(i.next());

      // Persist the media
      session.saveOrUpdate(media);

    } catch (HibernateException pe) {
      System.err.println("Problème dans la sauvegarde ");
      pe.printStackTrace();
      return false;
    }
    return true;
  }
 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;
 }
예제 #6
0
  @SuppressWarnings("unchecked")
  public Boolean delMedia(Session session, Media media) {
    try {
      // Need to delete the loans before deleting the media
      Map<Integer, Loan> tabEx = media.getTabExemplaries();
      for (Iterator<Loan> i = tabEx.values().iterator(); i.hasNext(); ) session.delete(i.next());

      // Also, all the bookings of the media need to be deleted
      List<Booking> bookings =
          (List<Booking>)
              session.createQuery("from Booking where media=:m").setParameter("m", media).list();
      for (Booking b : bookings) session.delete(b);

      session.delete(media);

    } catch (HibernateException pe) {
      System.err.println("Problème dans la suppression ");
      pe.printStackTrace();
      return false;
    }
    return true;
  }
예제 #7
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();
  }