Search in sources :

Example 31 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class EntityBasedAssociationAttribute method getAssociationKey.

@Override
public AssociationKey getAssociationKey() {
    final AssociationType type = getType();
    if (type.isAnyType()) {
        return new AssociationKey(JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()), JoinHelper.getLHSColumnNames(type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
    }
    final Joinable joinable = type.getAssociatedJoinable(sessionFactory());
    if (type.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
        final String lhsTableName;
        final String[] lhsColumnNames;
        if (joinable.isCollection()) {
            final QueryableCollection collectionPersister = (QueryableCollection) joinable;
            lhsTableName = collectionPersister.getTableName();
            lhsColumnNames = collectionPersister.getElementColumnNames();
        } else {
            final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
            lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
            lhsColumnNames = getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
        }
        return new AssociationKey(lhsTableName, lhsColumnNames);
    } else {
        return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
    }
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 32 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class MetamodelGraphWalker method visitCollectionElements.

private void visitCollectionElements(CollectionDefinition collectionDefinition) {
    final CollectionElementDefinition elementDefinition = collectionDefinition.getElementDefinition();
    strategy.startingCollectionElements(elementDefinition);
    final Type collectionElementType = elementDefinition.getType();
    if (collectionElementType.isAnyType()) {
        visitAnyDefinition(elementDefinition.toAnyMappingDefinition());
    } else if (collectionElementType.isComponentType()) {
        visitCompositeDefinition(elementDefinition.toCompositeElementDefinition());
    } else if (collectionElementType.isEntityType()) {
        if (!collectionDefinition.getCollectionPersister().isOneToMany()) {
            final QueryableCollection queryableCollection = (QueryableCollection) collectionDefinition.getCollectionPersister();
            addAssociationKey(new AssociationKey(queryableCollection.getTableName(), queryableCollection.getElementColumnNames()));
        }
        visitEntityDefinition(elementDefinition.toEntityDefinition());
    }
    strategy.finishingCollectionElements(elementDefinition);
}
Also used : Type(org.hibernate.type.Type) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 33 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class PersistentListTest method testInverseListIndex2.

@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseListIndex2() {
    // make sure no one changes the mapping
    final CollectionPersister collectionPersister = sessionFactory().getCollectionPersister(Order.class.getName() + ".lineItems");
    assertTrue(collectionPersister.isInverse());
    // do some creations...
    Session session = openSession();
    session.beginTransaction();
    Order order = new Order("acme-1");
    order.addLineItem("abc", 2);
    order.addLineItem("def", 200);
    order.addLineItem("ghi", 13);
    session.save(order);
    session.getTransaction().commit();
    session.close();
    // now, make sure the list-index column gotten written...
    final Session session2 = openSession();
    session2.beginTransaction();
    session2.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            final QueryableCollection queryableCollection = (QueryableCollection) collectionPersister;
            SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("ORDER_ID").addColumn("INDX").addColumn("PRD_CODE");
            PreparedStatement preparedStatement = ((SessionImplementor) session2).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
            ResultSet resultSet = ((SessionImplementor) session2).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
            Map<String, Integer> valueMap = new HashMap<String, Integer>();
            while (resultSet.next()) {
                final int fk = resultSet.getInt(1);
                assertFalse("Collection key (FK) column was null", resultSet.wasNull());
                final int indx = resultSet.getInt(2);
                assertFalse("List index column was null", resultSet.wasNull());
                final String prodCode = resultSet.getString(3);
                assertFalse("Prod code column was null", resultSet.wasNull());
                valueMap.put(prodCode, indx);
            }
            assertEquals(3, valueMap.size());
            assertEquals(Integer.valueOf(0), valueMap.get("abc"));
            assertEquals(Integer.valueOf(1), valueMap.get("def"));
            assertEquals(Integer.valueOf(2), valueMap.get("ghi"));
        }
    });
    session2.delete(order);
    session2.getTransaction().commit();
    session2.close();
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PreparedStatement(java.sql.PreparedStatement) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SimpleSelect(org.hibernate.sql.SimpleSelect) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

QueryableCollection (org.hibernate.persister.collection.QueryableCollection)33 Type (org.hibernate.type.Type)10 QueryException (org.hibernate.QueryException)9 AssociationType (org.hibernate.type.AssociationType)7 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 Joinable (org.hibernate.persister.entity.Joinable)6 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 HashMap (java.util.HashMap)4 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)4 EntityType (org.hibernate.type.EntityType)4 SemanticException (antlr.SemanticException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Map (java.util.Map)3 Session (org.hibernate.Session)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3