hibernate/hibernate-orm · error · UnsupportedOperationException

any types do not have a unique referenced persister

Error message

any types do not have a unique referenced persister

What it means

AnyType implements AssociationType but cannot honor getAssociatedJoinable, because an @Any reference points at many possible entity persisters depending on the discriminator value, so there is no single Joinable to resolve. AnyType.getAssociatedJoinable (AnyType.java:514) therefore throws UnsupportedOperationException whenever the query/AST machinery asks for the joinable behind an any-typed association.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/AnyType.java:515

	}

	public boolean isReferenceToPrimaryKey() {
		return true;
	}

	@Override
	public String getRHSUniqueKeyPropertyName() {
		return null;
	}

	@Override
	public boolean isAlwaysDirtyChecked() {
		return false;
	}

	@Override
	public Joinable getAssociatedJoinable(SessionFactoryImplementor factory) {
		throw new UnsupportedOperationException("any types do not have a unique referenced persister");
	}

	@Override
	public String getAssociatedEntityName(SessionFactoryImplementor factory) {
		throw new UnsupportedOperationException("any types do not have a unique referenced persister");
	}

	/**
	 * Used to externalize discrimination per a given identifier.  For example, when writing to
	 * second level cache we write the discrimination resolved concrete type for each entity written.
	 */
	public static final class ObjectTypeCacheEntry implements Serializable {
		final String entityName;
		final Object id;

		ObjectTypeCacheEntry(String entityName, Object id) {
			this.entityName = entityName;
			this.id = id;

View on GitHub (pinned to fad1729dce)

Solutions

  1. Remove joins over the @Any attribute; filter with its 'class' and 'id' sub-attributes and resolve targets with a second query or batch load.
  2. Replace @Any with real associations: one @ManyToOne per target type, or an intermediate link entity (table per relationship with discriminator + real FK) that can be joined.
  3. For @ManyToAny collections, iterate elements and operate on the concrete types instead of joining the collection in HQL.
  4. If you control the generic query layer, exclude any-typed attributes from automatic join generation.

Example fix

// before
List<Doc> docs = session.createQuery(
    "select d from Doc d join d.owner o where o.status = :s", Doc.class)
    .setParameter("s", Status.ACTIVE).getResultList();

// after: no join across @Any; two-step resolution
List<Doc> docs = session.createQuery(
    "select d from Doc d where d.owner.class = User and d.owner.id in :ids", Doc.class)
    .setParameter("ids", activeUserIds).getResultList();
Defensive patterns

Strategy: type-guard

Validate before calling

// Before building joins generically, skip any-typed associations
Type t = persister.getPropertyType(propName);
if (t instanceof org.hibernate.type.AnyType) {
    // no joinable exists: filter via class/id or restructure the mapping
    continue;
}

Type guard

static boolean isJoinableAssociation(Type t) {
    return t instanceof AssociationType && !(t instanceof org.hibernate.type.AnyType);
}

Try / catch

try {
    return session.createQuery("select d from Doc d join d.owner o", Doc.class).getResultList();
} catch (UnsupportedOperationException e) {
    if (e.getMessage().contains("unique referenced persister")) {
        throw new QueryDesignException(
            "Cannot join across @Any attribute 'owner'; query via class+id instead", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: HQL or Criteria that (implicitly or explicitly) JOINs across an @Any/@ManyToAny attribute, e.g. 'select d from Doc d join d.owner o where o.status = :s'; fetch-joining an @Any; ORDER BY / GROUP BY a nested path that forces an implicit join; programmatic calls to getAssociatedJoinable on the type of an any-mapped property.

Common situations: Teams migrating queries written for @ManyToOne after switching the mapping to @Any for polymorphism; generic query builders / specifications that join every referenced path; Envers or reporting tools walking associations uniformly and expecting a single target persister.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/3a463cd53fa55f3f. Report an issue: GitHub.