Search in sources :

Example 16 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class OnLockVisitor method processCollection.

@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == null) {
        return null;
    }
    final SessionImplementor session = getSession();
    final CollectionPersister persister = session.getFactory().getCollectionPersister(type.getRole());
    if (collection instanceof PersistentCollection) {
        final PersistentCollection persistentCollection = (PersistentCollection) collection;
        if (persistentCollection.setCurrentSession(session)) {
            if (isOwnerUnchanged(persistentCollection, persister, extractCollectionKeyFromOwner(persister))) {
                // a "detached" collection that originally belonged to the same entity
                if (persistentCollection.isDirty()) {
                    throw new HibernateException("reassociated object has dirty collection");
                }
                reattachCollection(persistentCollection, type);
            } else {
                // a "detached" collection that belonged to a different entity
                throw new HibernateException("reassociated object has dirty collection reference");
            }
        } else {
            // to the entity passed to update()
            throw new HibernateException("reassociated object has dirty collection reference");
        }
    } else {
        //TODO: or an array!! we can't lock objects with arrays now??
        throw new HibernateException("reassociated object has dirty collection reference (or an array)");
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 17 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class AbstractManipulationCriteriaQuery method interpret.

@Override
public CriteriaInterpretation interpret(RenderingContext renderingContext) {
    final String jpaqlString = renderQuery(renderingContext);
    return new CriteriaInterpretation() {

        @Override
        @SuppressWarnings("unchecked")
        public QueryImplementor buildCompiledQuery(SessionImplementor entityManager, final InterpretedParameterMetadata interpretedParameterMetadata) {
            final Map<String, Class> implicitParameterTypes = extractTypeMap(interpretedParameterMetadata.implicitParameterBindings());
            QueryImplementor query = entityManager.createQuery(jpaqlString, null, null, new HibernateEntityManagerImplementor.QueryOptions() {

                @Override
                public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
                    return null;
                }

                @Override
                public Map<String, Class> getNamedParameterExplicitTypes() {
                    return implicitParameterTypes;
                }

                @Override
                public ResultMetadataValidator getResultMetadataValidator() {
                    return null;
                }
            });
            for (ImplicitParameterBinding implicitParameterBinding : interpretedParameterMetadata.implicitParameterBindings()) {
                implicitParameterBinding.bind(query);
            }
            return query;
        }

        private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {
            final HashMap<String, Class> map = new HashMap<>();
            for (ImplicitParameterBinding implicitParameter : implicitParameterBindings) {
                map.put(implicitParameter.getParameterName(), implicitParameter.getJavaType());
            }
            return map;
        }
    };
}
Also used : HashMap(java.util.HashMap) CriteriaInterpretation(org.hibernate.query.criteria.internal.compile.CriteriaInterpretation) HibernateEntityManagerImplementor(org.hibernate.jpa.spi.HibernateEntityManagerImplementor) InterpretedParameterMetadata(org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) List(java.util.List) QueryImplementor(org.hibernate.query.spi.QueryImplementor) HashMap(java.util.HashMap) Map(java.util.Map) ImplicitParameterBinding(org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding)

Example 18 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class CompositeDerivedIdentityTest method testBidirectonalKeyManyToOneId.

@Test
@TestForIssue(jiraKey = "HHH-10476")
public void testBidirectonalKeyManyToOneId() {
    Product product = new Product();
    product.setName("Product 1");
    Session session = openSession();
    session.beginTransaction();
    session.save(product);
    session.getTransaction().commit();
    session.close();
    Order order = new Order();
    order.setName("Order 1");
    order.addLineItem(product, 2);
    session = openSession();
    session.beginTransaction();
    session.save(order);
    session.getTransaction().commit();
    session.close();
    session = openSession();
    session.beginTransaction();
    OrderLine orderLine = order.getLineItems().iterator().next();
    orderLine.setAmount(5);
    OrderLine orderLineGotten = session.get(OrderLine.class, orderLine);
    assertSame(orderLineGotten, orderLine);
    assertEquals(Integer.valueOf(2), orderLineGotten.getAmount());
    SessionImplementor si = (SessionImplementor) session;
    assertTrue(si.getPersistenceContext().isEntryFor(orderLineGotten));
    assertFalse(si.getPersistenceContext().isEntryFor(orderLineGotten.getOrder()));
    assertFalse(si.getPersistenceContext().isEntryFor(orderLineGotten.getProduct()));
    session.getTransaction().commit();
    session.close();
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class EnumeratedTypeTest method testFormula.

@Test
@TestForIssue(jiraKey = "HHH-9369")
public void testFormula() throws SQLException {
    // use native SQL to insert, forcing whitespace to occur
    final Session s = openSession();
    final Connection connection = ((SessionImplementor) s).connection();
    final Statement statement = connection.createStatement();
    statement.execute("insert into EntityEnum (id) values(1)");
    s.getTransaction().begin();
    // ensure EnumType can do #fromName with the trimming
    List<EntityEnum> resultList = s.createQuery("select e from EntityEnum e").list();
    assertEquals(resultList.size(), 1);
    assertEquals(resultList.get(0).getFormula(), Trimmed.A);
    statement.execute("delete from EntityEnum");
    s.getTransaction().commit();
    s.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 20 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class EnumeratedTypeTest method testTrimmedEnumChar.

@Test
@TestForIssue(jiraKey = "HHH-4699")
@SkipForDialect(value = { Oracle8iDialect.class, AbstractHANADialect.class }, jiraKey = "HHH-8516", comment = "HHH-4699 was specifically for using a CHAR, but Oracle/HANA do not handle the 2nd query correctly without VARCHAR. ")
public void testTrimmedEnumChar() throws SQLException {
    // use native SQL to insert, forcing whitespace to occur
    final Session s = openSession();
    final Connection connection = ((SessionImplementor) s).connection();
    final Statement statement = connection.createStatement();
    statement.execute("insert into EntityEnum (id, trimmed) values(1, '" + Trimmed.A.name() + "')");
    statement.execute("insert into EntityEnum (id, trimmed) values(2, '" + Trimmed.B.name() + "')");
    s.getTransaction().begin();
    // ensure EnumType can do #fromName with the trimming
    List<EntityEnum> resultList = s.createQuery("select e from EntityEnum e").list();
    assertEquals(resultList.size(), 2);
    assertEquals(resultList.get(0).getTrimmed(), Trimmed.A);
    assertEquals(resultList.get(1).getTrimmed(), Trimmed.B);
    // ensure querying works
    final Query query = s.createQuery("select e from EntityEnum e where e.trimmed=?");
    query.setParameter(0, Trimmed.A);
    resultList = query.list();
    assertEquals(resultList.size(), 1);
    assertEquals(resultList.get(0).getTrimmed(), Trimmed.A);
    statement.execute("delete from EntityEnum");
    s.getTransaction().commit();
    s.close();
}
Also used : Query(org.hibernate.Query) Statement(java.sql.Statement) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)82 Session (org.hibernate.Session)59 Test (org.junit.Test)54 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 EntityEntry (org.hibernate.engine.spi.EntityEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)6