use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class SQLExceptionConversionTest method testIntegrityViolation.
@Test
@SkipForDialect(value = { MySQLMyISAMDialect.class, AbstractHANADialect.class }, comment = "MySQL (MyISAM) / Hana do not support FK violation checking")
public void testIntegrityViolation() throws Exception {
final Session session = openSession();
session.beginTransaction();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// Attempt to insert some bad values into the T_MEMBERSHIP table that should
// result in a constraint violation
PreparedStatement ps = null;
try {
ps = ((SessionImplementor) session).getJdbcCoordinator().getStatementPreparer().prepareStatement("INSERT INTO T_MEMBERSHIP (user_id, group_id) VALUES (?, ?)");
// Non-existent user_id
ps.setLong(1, 52134241);
// Non-existent group_id
ps.setLong(2, 5342);
((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
fail("INSERT should have failed");
} catch (ConstraintViolationException ignore) {
// expected outcome
} finally {
releaseStatement(session, ps);
}
}
});
session.getTransaction().rollback();
session.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class PooledHiLoSequenceIdentifierTest method insertNewRow.
private void insertNewRow(Session session) {
final SessionImplementor si = (SessionImplementor) session;
final SessionFactoryImplementor sfi = si.getFactory();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement("INSERT INTO sequenceIdentifier VALUES (?)");
statement.setObject(1, sfi.getIdentifierGenerator(SequenceIdentifier.class.getName()).generate(si, null));
statement.executeUpdate();
} finally {
if (statement != null) {
statement.close();
}
}
}
});
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class HQLScrollFetchTest method testIncompleteScrollLast.
@Test
@TestForIssue(jiraKey = "HHH-1283")
public void testIncompleteScrollLast() {
Session s = openSession();
s.beginTransaction();
ScrollableResults results = s.createQuery(QUERY + " order by p.name asc").scroll();
results.next();
Parent p = (Parent) results.get(0);
assertResultFromOneUser(p);
results.last();
// get the other parent entity from the persistence context.
// since the result set was scrolled to the end, the other parent entity's collection has been
// properly initialized.
Parent pOther = null;
Set childrenOther = new HashSet();
for (Object entity : ((SessionImplementor) s).getPersistenceContext().getEntitiesByKey().values()) {
if (Parent.class.isInstance(entity)) {
if (entity != p) {
if (pOther != null) {
fail("unexpected parent found.");
}
pOther = (Parent) entity;
}
} else if (Child.class.isInstance(entity)) {
if (!p.getChildren().contains(entity)) {
childrenOther.add(entity);
}
} else {
fail("unexpected type of entity.");
}
}
// check that the same second parent is obtained by calling Session.get()
assertNotNull(pOther);
assertSame(pOther, s.get(Parent.class, pOther.getId()));
// access pOther's collection; should be completely loaded
assertTrue(Hibernate.isInitialized(pOther.getChildren()));
assertEquals(childrenOther, pOther.getChildren());
assertResultFromOneUser(pOther);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class EntityAssociationResultSetProcessorTest method testNestedManyToOneEntityProcessing.
@Test
public void testNestedManyToOneEntityProcessing() throws Exception {
final EntityPersister entityPersister = sessionFactory().getEntityPersister(ReportedMessage.class.getName());
// create some test data
Session session = openSession();
session.beginTransaction();
Message message = new Message(1, "the message");
Poster poster = new Poster(2, "the poster");
session.save(message);
session.save(poster);
message.poster = poster;
poster.messages.add(message);
ReportedMessage reportedMessage = new ReportedMessage(0, "inappropriate", message);
session.save(reportedMessage);
session.getTransaction().commit();
session.close();
{
final LoadPlan plan = Helper.INSTANCE.buildLoadPlan(sessionFactory(), entityPersister);
final LoadQueryDetails queryDetails = Helper.INSTANCE.buildLoadQueryDetails(plan, sessionFactory());
final String sql = queryDetails.getSqlStatement();
final ResultSetProcessor resultSetProcessor = queryDetails.getResultSetProcessor();
final List results = new ArrayList();
final Session workSession = openSession();
workSession.beginTransaction();
workSession.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, 0);
ResultSet resultSet = ps.executeQuery();
results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, new QueryParameters(), new NamedParameterContext() {
@Override
public int[] getNamedParameterLocations(String name) {
return new int[0];
}
}, true, false, null, null));
resultSet.close();
ps.close();
}
});
assertEquals(1, results.size());
Object result = results.get(0);
assertNotNull(result);
ReportedMessage workReportedMessage = ExtraAssertions.assertTyping(ReportedMessage.class, result);
assertEquals(0, workReportedMessage.id.intValue());
assertEquals("inappropriate", workReportedMessage.reason);
Message workMessage = workReportedMessage.message;
assertNotNull(workMessage);
assertTrue(Hibernate.isInitialized(workMessage));
assertEquals(1, workMessage.mid.intValue());
assertEquals("the message", workMessage.msgTxt);
assertTrue(Hibernate.isInitialized(workMessage.poster));
Poster workPoster = workMessage.poster;
assertEquals(2, workPoster.pid.intValue());
assertEquals("the poster", workPoster.name);
assertFalse(Hibernate.isInitialized(workPoster.messages));
workSession.getTransaction().commit();
workSession.close();
}
// clean up test data
session = openSession();
session.beginTransaction();
session.createQuery("delete ReportedMessage").executeUpdate();
session.createQuery("delete Message").executeUpdate();
session.createQuery("delete Poster").executeUpdate();
session.getTransaction().commit();
session.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class EntityWithNonLazyOneToManyListResultSetProcessorTest method testEntityWithList.
@Test
public void testEntityWithList() throws Exception {
final EntityPersister entityPersister = sessionFactory().getEntityPersister(Poster.class.getName());
// create some test data
Session session = openSession();
session.beginTransaction();
Poster poster = new Poster();
poster.pid = 0;
poster.name = "John Doe";
Message message1 = new Message();
message1.mid = 1;
message1.msgTxt = "Howdy!";
message1.poster = poster;
poster.messages.add(message1);
Message message2 = new Message();
message2.mid = 2;
message2.msgTxt = "Bye!";
message2.poster = poster;
poster.messages.add(message2);
session.save(poster);
session.getTransaction().commit();
session.close();
// session = openSession();
// session.beginTransaction();
// Poster posterGotten = (Poster) session.get( Poster.class, poster.pid );
// assertEquals( 0, posterGotten.pid.intValue() );
// assertEquals( poster.name, posterGotten.name );
// assertTrue( Hibernate.isInitialized( posterGotten.messages ) );
// assertEquals( 2, posterGotten.messages.size() );
// assertEquals( message1.msgTxt, posterGotten.messages.get( 0 ).msgTxt );
// assertEquals( message2.msgTxt, posterGotten.messages.get( 1 ).msgTxt );
// assertSame( posterGotten, posterGotten.messages.get( 0 ).poster );
// assertSame( posterGotten, posterGotten.messages.get( 1 ).poster );
// session.getTransaction().commit();
// session.close();
{
final LoadPlan plan = Helper.INSTANCE.buildLoadPlan(sessionFactory(), entityPersister);
final LoadQueryDetails queryDetails = Helper.INSTANCE.buildLoadQueryDetails(plan, sessionFactory());
final String sql = queryDetails.getSqlStatement();
final ResultSetProcessor resultSetProcessor = queryDetails.getResultSetProcessor();
final List results = new ArrayList();
final Session workSession = openSession();
workSession.beginTransaction();
workSession.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, 0);
ResultSet resultSet = ps.executeQuery();
results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, new QueryParameters(), new NamedParameterContext() {
@Override
public int[] getNamedParameterLocations(String name) {
return new int[0];
}
}, true, false, null, null));
resultSet.close();
ps.close();
}
});
assertEquals(2, results.size());
Object result1 = results.get(0);
assertNotNull(result1);
assertSame(result1, results.get(1));
Poster workPoster = ExtraAssertions.assertTyping(Poster.class, result1);
assertEquals(0, workPoster.pid.intValue());
assertEquals(poster.name, workPoster.name);
assertTrue(Hibernate.isInitialized(workPoster.messages));
assertEquals(2, workPoster.messages.size());
assertTrue(Hibernate.isInitialized(workPoster.messages));
assertEquals(2, workPoster.messages.size());
assertEquals(message1.msgTxt, workPoster.messages.get(0).msgTxt);
assertEquals(message2.msgTxt, workPoster.messages.get(1).msgTxt);
assertSame(workPoster, workPoster.messages.get(0).poster);
assertSame(workPoster, workPoster.messages.get(1).poster);
workSession.getTransaction().commit();
workSession.close();
}
// clean up test data
session = openSession();
session.beginTransaction();
session.delete(poster);
session.getTransaction().commit();
session.close();
}
Aggregations