Hi Tobi,
theoretically, you could use the schema session to release an entity:
Session session = entity.getSession();
session.release(entitiy);
session.commit();
But - depending on your project - there might be some drawbacks...
When releasing an entity "directly", the <RULES> will not be checked because they are part of the table template which is the "glue" between an entity and a dataset.
You might also want to further check outgoing references of the entity/dataset - of course that's also possible for an entity via Schema.getOutgoingReferences(...) - but in case of a Dataset you'd also have an IDProvider (more precisely: StoreElement) where you can also just use .getOutouingReferences (like for other elements). So you could handle this more "generally" without having to distinguish between cases (only relevant if you are doing more reference checks also for other elements).
Of course, iterating through all datasources for every entity is not that optimal. One idea would be to load the datasources upfront (and create some kind of mapping).
Also, as you just use the first found Datasource, that might (depending on your project) not the best one (e.g. without rules). What's the "best" approach always depends on your project.
A quite common alternative approach is not to use a Datasource (Content2) to create the Dataset but a TableTemplate (both are DatasetProviders and have the getEntity method). The idea would be to iterate through the Schema's tabletemplates and filter for those having the matching EntityType. Among those, you could use
- the first one that has a preview page defined (because in many projects, that is usually the template with all input components and thus also rules), or
- the one with the highest number of input components
- ... etc.
Hope this helps 😉
Michael