StatelessSession session = sessionFactory.openStatelessSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Item item = new Item(...); session.insert(item); } tx.commit(); session.close();
@Test public void testInsertWithForeignKey() { Session session = sessionFactory().openSession(); Transaction tx = session.beginTransaction(); Message msg = new Message(); final String messageId = "message_id"; msg.setId(messageId); msg.setContent("message_content"); msg.setSubject("message_subject"); session.save(msg); tx.commit(); session.close(); StatelessSession statelessSession = sessionFactory().openStatelessSession(); tx = statelessSession.beginTransaction(); MessageRecipient signature = new MessageRecipient(); signature.setId("recipient"); signature.setEmail("recipient@hibernate.org"); signature.setMessage(msg); statelessSession.insert(signature); tx.commit(); cleanup(); }
@Test @TestForIssue( jiraKey = "HHH-11732" ) public void test() { Triggerable triggerable = logInspection.watchForLogMessages( "HHH000352" ); triggerable.reset(); StatelessSession session = entityManagerFactory().unwrap( SessionFactory.class ).openStatelessSession(); Transaction tx = session.beginTransaction(); try { Employee employee = new Employee( "1", "2", 1 ); employee.setId( 1 ); session.insert( employee ); tx.rollback(); } catch (HibernateException e) { if ( tx != null ) { tx.rollback(); } } finally { session.close(); assertFalse( triggerable.wasTriggered() ); } }
@Override public Serializable insert(Object entity) { return this.raw.insert(entity); }
@Override public void performOperation(Object object, StatelessSession session) { session.insert(object); } }
@Override public Serializable insert(String entityName, Object entity) { return this.raw.insert(entityName, entity); }
id = s.insert(entity);
public void addMessage(InternalClusterMessage message) { withStatelessSession(session -> { session.insert(message); return null; }); }
public boolean create(InternalClusterJob job) { try { withStatelessSession(session -> { session.insert(job); return null; }); return true; } catch (ConstraintViolationException e) { return false; } }
public void insert(T object) { sessionFactory.openStatelessSession().insert(object); }
@Override public void insert(List<ChannelSeverity> channelSeverities) { StatelessSession statelessSession = sessionFactory.openStatelessSession(); try { for (ChannelSeverity channelSeverity : channelSeverities) { statelessSession.insert(channelSeverity); } } finally { statelessSession.close(); } }
@Override public void saveOrUpdateStateless(ChannelVulnerability channelVulnerability) { StatelessSession statelessSession = sessionFactory.openStatelessSession(); statelessSession.insert(channelVulnerability); statelessSession.close(); }
/** * Attempt to insert an "unlocked" DB row for the named lock. * * @param lockName the lock identifier * @param updateTime the time in milliseconds that this insert was written */ public void insertEmptyClusterLock(@Nonnull String lockName, long updateTime) { try { withStatelessSession(statelessSession -> { statelessSession.insert(new ClusterLockEntity(lockName, null, updateTime)); log.debug("Inserted empty lock {}", lockName); return null; } ); } catch (ConstraintViolationException e) { log.debug("Didn't insert empty lock {}, because lock already exists", lockName); } }
/** * Tries to insert a new heartbeat based on the parameters. Should be used on node startup, to guarantee a * unique nodeId. If the node id provided is already used, will call the supplier multiple times to obtain one that * isn't. * * @param nodeIdSupplier a supplier of unique node ids * @param nodeName the desired node names * @param timestamp the desired timestamp of the heartbeat * @return the node id for the successfully inserted heartbeat * @throws OperationFailedException if insertion fails despite retries */ public String writeNewHeartbeat(final Supplier<String> nodeIdSupplier, final String nodeName, final long timestamp) throws OperationFailedException { for (int retry = 0; retry < 100; retry++) { final String nodeId = nodeIdSupplier.get(); try { log.debug("Trying to write node heartbeat with node id: {}", nodeId); return (String) withStatelessSession(statelessSession -> statelessSession.insert(new ClusterHeartbeatEntity(nodeId, nodeName, timestamp))); } catch (ConstraintViolationException e) { log.debug("Failed writing heartbeat with node id {}", nodeId); } } throw new OperationFailedException("Unable to write unique node id"); }
if (books == null || books.isEmpty) { return; } StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession(); Transaction tx = session.beginTransaction(); for (Book each : books) { session.insert(book); } tx.commit(); session.close();
@Override public void insertAll(List<GenericVulnerability> vulnerabilityList) { StatelessSession statelessSession = sessionFactory.openStatelessSession(); Transaction transaction = statelessSession.beginTransaction(); try { for (GenericVulnerability vulnerability : vulnerabilityList) { statelessSession.insert(vulnerability); } transaction.commit(); } catch (Exception e) { transaction.rollback(); } finally { statelessSession.close(); } } }
StatelessSession session = sessionFactory.openStatelessSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Item item = new Item(.....); session.insert(item ); } tx.commit(); session.close();
id = s.insert(entity);
/** * Inserts or updates the heartbeat entity */ public void writeHeartBeat(ClusterHeartbeatEntity heartbeatEntity) throws DataAccessException { withStatelessSession(statelessSession -> { final int updated = statelessSession.createQuery("update ClusterHeartbeatEntity hb " + "set hb.nodeName = :nodeName, hb.timestamp = :timestamp " + "where hb.nodeId = :nodeId") .setParameter("nodeName", heartbeatEntity.getNodeName()) .setParameter("timestamp", heartbeatEntity.getTimestamp()) .setParameter("nodeId", heartbeatEntity.getNodeId()) .executeUpdate(); if (updated == 0) { statelessSession.insert(heartbeatEntity); } return null; }); }
id = s.insert(entity);