Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

In this page you can find the example usage for org.hibernate Session refresh.

Prototype

void refresh(Object object);

Source Link

Document

Re-read the state of the given instance from the underlying database.

Usage

From source file:storybook.model.EntityUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void fillEntityCombo(MainFrame mainFrame, JComboBox combo, AbstractEntityHandler entityHandler,
        AbstractEntity entity, boolean isNew, boolean addEmptyItem) {
    combo.removeAllItems();//from  w  ww  . j a va2s.com
    ListCellRenderer renderer = entityHandler.getListCellRenderer();
    if (renderer != null) {
        combo.setRenderer(renderer);
    }
    int i = 0;
    if (addEmptyItem) {
        ++i;
        combo.addItem("");
    }
    BookModel model = mainFrame.getBookModel();
    Session session = model.beginTransaction();
    SbGenericDAOImpl<?, ?> dao = entityHandler.createDAO();
    dao.setSession(session);
    @SuppressWarnings("unchecked")
    List<AbstractEntity> entities = (List<AbstractEntity>) dao.findAll();
    for (AbstractEntity entity2 : entities) {
        session.refresh(entity2);
        combo.addItem(entity2);
        if (entity != null) {
            if (entity.getId().equals(entity2.getId())) // don't use combo.setSelectedItem(entity) here
            // leads to a "no session" exception for tag links
            {
                combo.setSelectedIndex(i);
            }
        }
        ++i;
    }
    combo.revalidate();
    model.commit();
}

From source file:storybook.ui.dialog.copy.CopyDialog.java

License:Open Source License

@SuppressWarnings("unchecked")
private JPanel createEntitiesPanel() {
    MigLayout layout = new MigLayout();
    JPanel panel = new JPanel(layout);
    panel.setBorder(BorderFactory.createTitledBorder(I18N.getMsg("msg.copy.elements")));

    check = new CheckBoxPanel(mainFrame);
    JScrollPane scroller = new JScrollPane(check);
    SwingUtil.setUnitIncrement(scroller);
    SwingUtil.setMaxPreferredSize(scroller);
    panel.add(scroller, "grow");

    check.setAutoSelect(false);/*from  w ww . java  2 s . com*/
    check.setEntityHandler(copier.getEntityHandler(mainFrame));
    CbPanelDecorator decorator = copier.getDecorator();
    if (decorator != null) {
        decorator.setPanel(check);
        check.setDecorator(decorator);
    }

    BookModel model = mainFrame.getBookModel();
    Session session = model.beginTransaction();
    List<AbstractEntity> buf = new ArrayList<>();
    check.setEntityList((List<AbstractEntity>) copier.getAllElements(session, mainFrame));
    for (AbstractEntity entity : copier.getAllElements(session, mainFrame)) {
        check.addEntity(session, entity);
        buf.add(entity);
    }
    for (AbstractEntity entity : buf) {
        List<Attribute> attributes = EntityUtil.getEntityAttributes(mainFrame, entity);
        for (Attribute attr : attributes) {
            session = model.beginTransaction();
            session.refresh(attr);
            System.out.println(attr.getKey());
        }
    }
    check.initAll();

    return panel;
}

From source file:storybook.ui.MainFrame.java

License:Open Source License

public Part getCurrentPart() {
    try {//  w ww  .j  a  va2 s .  c o  m
        Session session = bookModel.beginTransaction();
        if (currentPart == null) {
            PartDAOImpl dao = new PartDAOImpl(session);
            currentPart = dao.findFirst();
        } else {
            session.refresh(currentPart);
        }
        bookModel.commit();
        return currentPart;
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:storybook.ui.panel.attributes.AttributesPanel.java

License:Open Source License

@Override
public void initUi() {
    setLayout(new MigLayout("wrap 2,fillx", "[grow][]", ""));

    BookModel model = mainFrame.getBookModel();
    Session session = model.beginTransaction();
    AttributeDAOImpl dao = new AttributeDAOImpl(session);
    keys = dao.findKeys();//from   w  w  w  .  j  av  a  2  s  .  com

    for (Attribute attribute : attributes) {
        session.refresh(attribute);
        AttributePanel panel = new AttributePanel(attribute, keys);
        attrPanels.add(panel);
        add(panel);
        add(getRemoveButton(panel));
    }

    AttributePanel newAttrPanel = new AttributePanel(keys);
    attrPanels.add(newAttrPanel);
    add(newAttrPanel);
    add(getRemoveButton(newAttrPanel));

    model.commit();

    IconButton btAdd = new IconButton(getAddAction());
    btAdd.setText(I18N.getMsg("msg.common.add"));
    btAdd.setIcon(I18N.getIcon("icon.small.plus"));
    add(btAdd, "newline,span,gap 0 0 10 0");
}

From source file:storybook.ui.panel.info.InfoPanel.java

License:Open Source License

@Override
public void modelPropertyChange(PropertyChangeEvent evt) {
    // Object oldValue = evt.getOldValue();
    Object newValue = evt.getNewValue();
    String propName = evt.getPropertyName();

    if (BookController.CommonProps.REFRESH.check(propName)) {
        View newView = (View) newValue;
        View view = (View) getParent().getParent();
        if (view == newView) {
            refresh();/*from   www .  ja va 2  s  . c o m*/
        }
        return;
    }

    if (BookController.CommonProps.SHOW_INFO.check(propName)) {
        if (newValue instanceof AbstractEntity) {
            entity = (AbstractEntity) newValue;
            if (entity.isTransient()) {
                return;
            }
            BookModel model = mainFrame.getBookModel();
            Session session = model.beginTransaction();
            session.refresh(entity);
            model.commit();
            refreshInfo();
            return;
        }
        if (newValue instanceof DbFile) {
            StringBuilder buf = new StringBuilder();
            buf.append("<p><b>\n");
            buf.append(I18N.getMsgColon("msg.common.title"));
            buf.append("</b></p><p>\n");
            Internal internal = BookUtil.get(mainFrame, BookKey.TITLE, "");
            buf.append(internal.getStringValue());
            buf.append("</p><p style='padding-top:10px'><b>\n");
            buf.append(I18N.getMsgColon("msg.common.subtitle"));
            buf.append("</b></p><p>\n");
            internal = BookUtil.get(mainFrame, BookKey.SUBTITLE, "");
            buf.append(internal.getStringValue());
            buf.append("</p><p style='padding-top:10px'><b>\n");
            buf.append(I18N.getMsgColon("msg.common.author_s"));
            buf.append("</b></p><p>\n");
            internal = BookUtil.get(mainFrame, BookKey.AUTHOR, "");
            buf.append(internal.getStringValue());
            buf.append("</p><p style='padding-top:10px'><b>\n");
            buf.append(I18N.getMsgColon("msg.common.copyright"));
            buf.append("</b></p><p>\n");
            internal = BookUtil.get(mainFrame, BookKey.COPYRIGHT, "");
            buf.append(internal.getStringValue());
            buf.append("</p><p style='padding-top:10px'><b>\n");
            buf.append(I18N.getMsgColon("msg.common.blurb"));
            buf.append("</b></p><p>\n");
            internal = BookUtil.get(mainFrame, BookKey.BLURB, "");
            buf.append(internal.getStringValue());
            buf.append("</p><p style='padding-top:10px'><b>\n");
            buf.append(I18N.getMsgColon("msg.common.notes"));
            buf.append("</b></p><p>\n");
            internal = BookUtil.get(mainFrame, BookKey.NOTES, "");
            buf.append(internal.getStringValue());
            buf.append("<p>\n");
            infoPane.setText(buf.toString());
            infoPane.setCaretPosition(0);
            return;
        }
    }

    if (entity != null && newValue instanceof AbstractEntity) {
        AbstractEntity updatedEntity = (AbstractEntity) newValue;
        if (updatedEntity.getId().equals(entity.getId())) {
            refreshInfo();
        }
    }
}

From source file:storybook.ui.panel.linkspanel.StrandLinksPanel.java

License:Open Source License

@Override
public void initUi() {
    setLayout(new MigLayout("insets 2"));
    if (opaque) {
        setOpaque(true);/*w w  w  .ja v  a 2 s .  c o  m*/
        setBackground(scene.getStrand().getJColor());
    } else {
        setOpaque(false);
    }
    BookModel model = mainFrame.getBookModel();
    Session session = model.beginTransaction();
    session.refresh(scene);
    List<Strand> list = scene.getStrands();
    if (list != null) {
        Collections.sort(list);
    }
    for (Strand strand : list) {
        try {
            session.refresh(strand);
        } catch (UnresolvableObjectException e) {
            e.printStackTrace();
            continue;
        }
        CleverLabel lb = new CleverLabel(strand.getAbbreviation(), JLabel.CENTER);
        lb.setToolTipText(EntityUtil.getToolTip(strand));
        lb.setBackground(strand.getJColor());
        add(lb, "w 30");
    }
    model.commit();
}

From source file:storybook.ui.table.renderer.AttributesTableCellRenderer.java

License:Open Source License

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    MainFrame mainFrame = (MainFrame) table.getClientProperty(ClientPropertyName.MAIN_FRAME.toString());
    JLabel lbText = (JLabel) super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row,
            column);/*from   www . ja va  2s . co  m*/
    if (value == null || value instanceof String) {
        return lbText;
    }
    try {
        @SuppressWarnings("unchecked")
        List<Attribute> list = (List<Attribute>) value;
        if (list == null || list.isEmpty()) {
            return lbText;
        }
        try {
            lbText.setText(StringUtils.join(list, ", "));
        } catch (NullPointerException e) {
            // ignore
        }
    } catch (LazyInitializationException lie) {
        BookModel model = mainFrame.getBookModel();
        Session session = model.beginTransaction();
        @SuppressWarnings("unchecked")
        List<Attribute> list = (List<Attribute>) value;
        try {
            for (Attribute property : list) {
                session.refresh(property);
            }
            lbText.setText(StringUtils.join(list, ", "));
            model.commit();
        } catch (Exception e) {
            // ignore
            // e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lbText;
}

From source file:storybook.ui.table.renderer.ChapterTableCellRenderer.java

License:Open Source License

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    JLabel lbText = (JLabel) super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row,
            column);//from  w  w w .  j  ava  2  s  .  c  o  m
    if (value == null || value instanceof String) {
        return lbText;
    }
    try {
        lbText.setText(value.toString());
    } catch (LazyInitializationException lie) {
        MainFrame mainFrame = (MainFrame) table.getClientProperty(ClientPropertyName.MAIN_FRAME.toString());
        BookModel model = mainFrame.getBookModel();
        Session session = model.beginTransaction();
        session.refresh((Chapter) value);
        lbText.setText(value.toString());
        model.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lbText;
}

From source file:storybook.ui.table.renderer.GenderTableCellRenderer.java

License:Open Source License

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    MainFrame mainFrame = (MainFrame) table.getClientProperty(ClientPropertyName.MAIN_FRAME.toString());
    JLabel lbText = (JLabel) super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row,
            column);//from w ww  .j a  v a2  s.  c  o  m
    if (value == null || value instanceof String || (!(value instanceof Gender))) {
        return lbText;
    }
    try {
        Gender gender = (Gender) value;
        lbText.setText(gender.toString());
        lbText.setIcon(gender.getIcon());
    } catch (LazyInitializationException lie) {
        BookModel model = mainFrame.getBookModel();
        Session session = model.beginTransaction();
        Gender gender = (Gender) value;
        session.refresh(gender);
        lbText.setText(gender.toString());
        lbText.setIcon(gender.getIcon());
        model.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lbText;
}

From source file:storybook.ui.table.renderer.ItemsTableCellRenderer.java

License:Open Source License

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    JLabel lbText = (JLabel) super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row,
            column);//from  ww w .j  a  va2s . c  om
    if (value instanceof String) {
        return lbText;
    }
    @SuppressWarnings("unchecked")
    List<Item> list = (List<Item>) value;
    try {
        lbText.setText(StringUtils.join(list, ", "));
    } catch (LazyInitializationException lie) {
        MainFrame mainFrame = (MainFrame) table.getClientProperty(ClientPropertyName.MAIN_FRAME.toString());
        BookModel model = mainFrame.getBookModel();
        Session session = model.beginTransaction();
        for (Item item : list) {
            session.refresh(item);
        }
        lbText.setText(StringUtils.join(list, ", "));
        model.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lbText;
}