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;
}
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;
}
};
}
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();
}
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();
}
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();
}
Aggregations