Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way in Hibernate to obtain an entity's loaded PersistentCollection without loading the entire entity object first?
    text
    copied!<p>This is a puzzler! :D</p> <p>Is there a way to force Hibernate to load a collection for an entity without loading the entire entity first?</p> <p>Let m explain better. I have a Role entity annotated this way:</p> <pre><code>@Entity(name="Role") @Table(name = "ROLES") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @javax.persistence.TableGenerator( name="GENERATED_IDS", table="GENERATED_IDS", valueColumnName = "ID" ) public abstract class Role implements Serializable { private static final long serialVersionUID = 1L; /** * The id of this role. Internal use only. * * @since 1.0 */ @Id @GeneratedValue(strategy = GenerationType.TABLE, generator="GENERATED_IDS") @Column(name = "ROLE_ID") protected long id; /** * Set of permissions granted to this role. * * @since 1.0 */ @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy="sourceRole") protected Set&lt;Permission&gt; permissions = new HashSet&lt;Permission&gt;(); ... } </code></pre> <p>When I access the permissions collection by doing:</p> <pre><code>Role role = ... Set permissions = role.getPermission(); </code></pre> <p>Hibernate wraps the returned collection with one of its subclasses of PersistentCollection. I can then use Hibernate.initialize(permissions); to force the collection to be initialized.</p> <p>However, what I need is a way to accomplish the same without first loading the role entity. I know the id for the entity I need the permissions collections for, and the Role for the collection (temp.pack.Role.permissions). </p> <p>Is there a way to do that? I wold like to avoid hitting the database to retrieve all fields of the Role object (many!) just to get the collection and discard them all.</p> <p>I could use a join, but that givens me access to the permission objects themselves, not the actual PersistentCollection wrapper which the one I need.</p> <p>I tried this:</p> <pre><code>Session session = connectionInfoProvider.getSession(); // this is just "permissions", the collection field name String attributeName = role.substring(role.lastIndexOf(".")+1, role.length()); // this is the Role class name, temp.pack.Role String entityClassName = role.substring(0, role.lastIndexOf(".")); Class&lt;?&gt; roleClass = Class.forName(entityClassName); Field collectionField = roleClass.getDeclaredField(attributeName); collectionField.setAccessible(true); Hibernate.initialize(collection); </code></pre> <p>But didn't work. The collection I get is just a regular empty set and nothing is loaded.</p> <p>I also tried this:</p> <pre><code>Session session = connectionInfoProvider.getSession(); // this is just "permissions", the collection field name String attributeName = role.substring(role.lastIndexOf(".")+1, role.length()); // this is the Role class name, temp.pack.Role String entityClassName = role.substring(0, role.lastIndexOf(".")); Class&lt;?&gt; roleClass = Class.forName(entityClassName); Field collectionField = roleClass.getDeclaredField(attributeName); collectionField.setAccessible(true); Object object = session.load(roleClass, id); ProxyObject objectProxy = (ProxyObject)object; LazyInitializer lazyInitializer = (LazyInitializer)objectProxy.getHandler(); Object objectImpl = lazyInitializer.getImplementation(); Object collection = collectionField.get(objectImpl); Hibernate.initialize(collection); </code></pre> <p>But also didn't work, it fails with <code>java.lang.IllegalArgumentException: unknown handler key</code>.</p> <p>I also tried:</p> <pre><code>PersistenceContext context = ((SessionImplementor)currSession).getPersistenceContext(); CollectionPersister persister = ((SessionFactoryImplementor) sessFactory) .getCollectionPersister(role); CollectionKey key = new CollectionKey(persister, id, EntityMode.POJO); // collection - contains set containing actual data PersistentCollection collection = context.getCollection(key); </code></pre> <p>But also fails with <code>java.lang.IllegalArgumentException: unknown handler key</code></p> <p>Any ideas on how to accomplish this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload