I’m working with Active Objects in my Jira plugin for data persistence. I keep seeing the @Preload annotation mentioned but I’m not sure what it actually does or when I should use it.
From what I understand, there might be some performance benefits, but I’d like to know more about how it affects database queries and object loading. Can someone explain the practical difference between using @Preload and not using it?
Also, are there any downsides to using this annotation that I should be aware of? I want to make sure I’m following best practices for my plugin development.
@Preload tells Active Objects to grab related entity data in one database query instead of lazy loading. Without it, AO lazy loads by default, which creates the N+1 query problem - you end up hitting the database multiple times.
I hit this on a project where I was showing a list of custom entities with their related data. Without @Preload, each entity fired off a separate query for its relationships. Performance tanked with larger datasets. Added @Preload to the relevant methods and boom - queries got consolidated and response times dropped dramatically.
Downside? You might fetch data you don’t need, eating up memory and making queries more complex. Only use it when you know you’ll actually access the related data. Watch out for deeply nested relationships too - those queries can get really heavy. It’s all about balancing query efficiency with resource usage.
@Preload changes how Active Objects handles relationships by forcing eager loading at query time. When you use @Preload, AO builds JOIN statements to grab related entities in one database hit instead of running separate queries later. I learned this the hard way building a reporting feature that showed project data with components. Without @Preload, the system ran one query for projects, then individual queries for each project’s components - classic N+1 that destroyed performance with hundreds of projects. The key is knowing when your code will actually use those relationships. @Preload works great when you’ll access related data right away, but it’s wasteful if you’re just showing summary info. I’ve seen devs preload everything “just in case” and end up with massive result sets that eat memory and actually slow things down. One gotcha: @Preload affects the entire query result, not specific fields. If you’re fetching 1000 entities but only need related data for 10, you’re still pulling everything.
@Preload was a lifesaver for my performance issues. It makes AO grab everything upfront instead of hitting the database multiple times later. Just don’t overdo it like I did - pulled way too much data and actually made things worse.