use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class ResultSetProcessingContextImpl method registerNonExists.
private void registerNonExists(EntityFetch fetch) {
final EntityType fetchedType = fetch.getFetchedType();
if (!fetchedType.isOneToOne()) {
return;
}
final EntityReferenceProcessingState fetchOwnerState = getOwnerProcessingState(fetch);
if (fetchOwnerState == null) {
throw new IllegalStateException("Could not locate fetch owner state");
}
final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
if (ownerEntityKey == null) {
throw new IllegalStateException("Could not locate fetch owner EntityKey");
}
session.getPersistenceContext().addNullProperty(ownerEntityKey, fetchedType.getPropertyName());
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class AbstractPropertyMapping method initIdentifierPropertyPaths.
protected void initIdentifierPropertyPaths(final String path, final EntityType etype, final String[] columns, final String[] columnReaders, final String[] columnReaderTemplates, final Mapping factory) throws MappingException {
Type idtype = etype.getIdentifierOrUniqueKeyType(factory);
String idPropName = etype.getIdentifierOrUniqueKeyPropertyName(factory);
boolean hasNonIdentifierPropertyNamedId = hasNonIdentifierPropertyNamedId(etype, factory);
if (etype.isReferenceToPrimaryKey()) {
if (!hasNonIdentifierPropertyNamedId) {
String idpath1 = extendPath(path, EntityPersister.ENTITY_ID);
addPropertyPath(idpath1, idtype, columns, columnReaders, columnReaderTemplates, null);
initPropertyPaths(idpath1, idtype, columns, columnReaders, columnReaderTemplates, null, factory);
}
}
if (idPropName != null) {
String idpath2 = extendPath(path, idPropName);
addPropertyPath(idpath2, idtype, columns, columnReaders, columnReaderTemplates, null);
initPropertyPaths(idpath2, idtype, columns, columnReaders, columnReaderTemplates, null, factory);
}
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class ManyToManyImplicitNamingTest method checkDefaultJoinTablAndJoinColumnNames.
protected void checkDefaultJoinTablAndJoinColumnNames(Class<?> ownerEntityClass, String ownerCollectionPropertyName, String inverseCollectionPropertyName, String expectedCollectionTableName, String ownerForeignKeyNameExpected, String inverseForeignKeyNameExpected) {
final org.hibernate.mapping.Collection collection = metadata().getCollectionBinding(ownerEntityClass.getName() + '.' + ownerCollectionPropertyName);
final org.hibernate.mapping.Table table = collection.getCollectionTable();
assertEquals(expectedCollectionTableName, table.getName());
final org.hibernate.mapping.Collection ownerCollection = metadata().getCollectionBinding(ownerEntityClass.getName() + '.' + ownerCollectionPropertyName);
// The default owner and inverse join columns can only be computed if they have PK with 1 column.
assertEquals(1, ownerCollection.getOwner().getKey().getColumnSpan());
assertEquals(ownerForeignKeyNameExpected, ownerCollection.getKey().getColumnIterator().next().getText());
final EntityType associatedEntityType = (EntityType) ownerCollection.getElement().getType();
final PersistentClass associatedPersistentClass = metadata().getEntityBinding(associatedEntityType.getAssociatedEntityName());
assertEquals(1, associatedPersistentClass.getKey().getColumnSpan());
if (inverseCollectionPropertyName != null) {
final org.hibernate.mapping.Collection inverseCollection = metadata().getCollectionBinding(associatedPersistentClass.getEntityName() + '.' + inverseCollectionPropertyName);
assertEquals(inverseForeignKeyNameExpected, inverseCollection.getKey().getColumnIterator().next().getText());
}
boolean hasOwnerFK = false;
boolean hasInverseFK = false;
for (Iterator it = ownerCollection.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) {
final ForeignKey fk = (ForeignKey) it.next();
assertSame(ownerCollection.getCollectionTable(), fk.getTable());
if (fk.getColumnSpan() > 1) {
continue;
}
if (fk.getColumn(0).getText().equals(ownerForeignKeyNameExpected)) {
assertSame(ownerCollection.getOwner().getTable(), fk.getReferencedTable());
hasOwnerFK = true;
} else if (fk.getColumn(0).getText().equals(inverseForeignKeyNameExpected)) {
assertSame(associatedPersistentClass.getTable(), fk.getReferencedTable());
hasInverseFK = true;
}
}
assertTrue(hasOwnerFK);
assertTrue(hasInverseFK);
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class FumTest method testCompositeIDQuery.
@Test
public void testCompositeIDQuery() throws Exception {
Session s = openSession();
s.beginTransaction();
Fum fee = new Fum(fumKey("fee", true));
fee.setFum("fee");
s.save(fee);
Fum fi = new Fum(fumKey("fi", true));
fi.setFum("fi");
short fiShort = fi.getId().getShort();
s.save(fi);
Fum fo = new Fum(fumKey("fo", true));
fo.setFum("fo");
s.save(fo);
Fum fum = new Fum(fumKey("fum", true));
fum.setFum("fum");
s.save(fum);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// Try to find the Fum object "fo" that we inserted searching by the string in the id
List vList = s.createQuery("from Fum fum where fum.id.string='fo'").list();
assertTrue("find by composite key query (find fo object)", vList.size() == 1);
fum = (Fum) vList.get(0);
assertTrue("find by composite key query (check fo object)", fum.getId().getString().equals("fo"));
// Try to find the Fum object "fi" that we inserted
vList = s.createQuery("from Fum fum where fum.id.short = ?").setParameter(0, new Short(fiShort), StandardBasicTypes.SHORT).list();
assertEquals("find by composite key query (find fi object)", 1, vList.size());
fi = (Fum) vList.get(0);
assertEquals("find by composite key query (check fi object)", "fi", fi.getId().getString());
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
assertTrue(s.createQuery("select fum.id.short, fum.id.string from Fum fum").iterate().hasNext());
assertTrue(s.createQuery("select fum.id from Fum fum").iterate().hasNext());
Query qu = s.createQuery("select fum.fum, fum , fum.fum from Fum fum");
Type[] types = qu.getReturnTypes();
assertTrue(types.length == 3);
for (int k = 0; k < types.length; k++) {
assertTrue(types[k] != null);
}
assertTrue(types[0] instanceof StringType);
assertTrue(types[1] instanceof EntityType);
assertTrue(types[2] instanceof StringType);
Iterator iter = qu.iterate();
int j = 0;
while (iter.hasNext()) {
j++;
assertTrue(((Object[]) iter.next())[1] instanceof Fum);
}
assertTrue("iterate on composite key", j == 8);
fum = (Fum) s.load(Fum.class, fum.getId());
s.createFilter(fum.getQuxArray(), "where this.foo is null").list();
s.createFilter(fum.getQuxArray(), "where this.foo.id = ?").setParameter(0, "fooid", StandardBasicTypes.STRING).list();
Query f = s.createFilter(fum.getQuxArray(), "where this.foo.id = :fooId");
f.setString("fooId", "abc");
assertFalse(f.iterate().hasNext());
iter = s.createQuery("from Fum fum where not fum.fum='FRIEND'").iterate();
int i = 0;
while (iter.hasNext()) {
fum = (Fum) iter.next();
s.delete(fum);
i++;
}
assertTrue("iterate on composite key", i == 4);
s.flush();
s.createQuery("from Fum fu, Fum fo where fu.fo.id.string = fo.id.string and fo.fum is not null").iterate();
s.createQuery("from Fumm f1 inner join f1.fum f2").list();
s.getTransaction().commit();
s.close();
}
use of org.hibernate.type.EntityType in project hibernate-orm by hibernate.
the class EntityGraphQueryHint method getFromElements.
private List<FromElement> getFromElements(List attributeNodes, FromElement origin, FromClause fromClause, HqlSqlWalker walker, Map<String, FromElement> explicitFetches) {
final List<FromElement> fromElements = new ArrayList<FromElement>();
for (Object obj : attributeNodes) {
final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;
final String attributeName = attributeNode.getAttributeName();
final String className = origin.getClassName();
// TODO: This is ignored by collection types and probably wrong for entity types. Presumably it screws
// with inheritance.
final String role = className + "." + attributeName;
final String classAlias = origin.getClassAlias();
final String originTableAlias = origin.getTableAlias();
final Type propertyType = origin.getPropertyType(attributeName, attributeName);
try {
FromElement fromElement = explicitFetches.get(role);
boolean explicitFromElement = false;
if (fromElement == null) {
if (propertyType.isEntityType()) {
final EntityType entityType = (EntityType) propertyType;
final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
final String tableAlias = walker.getAliasGenerator().createName(entityType.getAssociatedEntityName());
final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns);
fromElement = fromElementFactory.createEntityJoin(entityType.getAssociatedEntityName(), tableAlias, joinSequence, true, walker.isInFrom(), entityType, role, null);
} else if (propertyType.isCollectionType()) {
CollectionType collectionType = (CollectionType) propertyType;
final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
final QueryableCollection queryableCollection = walker.getSessionFactoryHelper().requireQueryableCollection(collectionType.getRole());
fromElement = fromElementFactory.createCollection(queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false);
}
} else {
explicitFromElement = true;
fromElement.setInProjectionList(true);
fromElement.setFetch(true);
}
if (fromElement != null) {
if (!explicitFromElement) {
fromElements.add(fromElement);
}
// recurse into subgraphs
for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
fromElements.addAll(getFromElements(subgraph.getAttributeNodes(), fromElement, fromClause, walker, explicitFetches));
}
}
} catch (Exception e) {
throw new QueryException("Could not apply the EntityGraph to the Query!", e);
}
}
return fromElements;
}
Aggregations