So I am writing an enterprise app using entity classes making use of a Java Persistence Manager, with Session beans implementing the business logic and providing remote access to my web application. This seems to be the recommended way to implement it.
Now, one of my entity beans is representing an object that belongs to an org structure style tree. That is, each entity has exactly one parent, but perhaps multiple children. This can obviously be implemented with @OneToMany and @ManyToOne style relationships. However, in this instance one of the most commonly used searches needs to match an entity to all of its descendents.
To give a simplified example using a room booking scenario
Suppose I have a hotel, with 3 ballrooms, each of which can be divided off into number of smaller 'sub-rooms'.
So, if someone wants to book one of the ballrooms, we must check that no-one has any of the smaller rooms booked for that time, as well as the the ballroom itself is not being used. Additionally, it is possible for someone to book the entire hotel out for a particular day, which would preclude anyone from booking any of the other 'sub-areas'.
So we have:
Room Entity (room_id, room_name, parent_room)
Booking (booking_id, customer_name, date, room_name)
Now, the way I have solved this problem (without using the persistence layer) is to create a third table which is automatically generated, which includes one row for each combination of room and ancestor - so a sub-room would contain one entry connecting itself to the ballroom, and another connecting it to the entire hotel. The fields are as follows:
Ancestor Entity (room_id, depth, ancestor_id)
Now obviously, the data in this table is redundant, and so should be automatically generated.
Now to my question which is basically - where should this automatic generation go?
Personally, I would like it to go into the entity bean structure itself, it is really a data implementation logic sort of thing, rather than being part of any business logic and therefore it shouldn't go in the session layer?
Please note also that in my application the org-structure data may also be updated frequently (orders of magnitude less frequently than the booking request queries, but frequently none the less).
My first attempt was to use the @PostUpdate and @PostPersist annotations to create these entities, but these methods seem unable to access the Persistance Manager and so, are unable to create and persist the new entities.
Before I write too much more and put everyone off, I'll end it here and wait for (hopefully) some responses.
Thankyou to anyone that can help me.