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