How to determine number of children of a record? - nhibernate

Good afternoon everyone!
I'm studying NHibernate, and decided to make some changes. Among them, I noticed that some fields are unnecessary. So I bring my doubt:
I have a list, let's call it Class_List within each study class, I can have N students for each class. Within the list Class_List, I also have other properties as simple as the name of the class.
How I see it is unnecessary to store how many students I have in the database, I would, in a single query, how many records I have. This, using NHibernate.
Is this possible? How?
Best regards,
Gustavo.
Edit: I've forgot to say one thing... I want to return this number of record, as a column. But this column is not mapped in my .hbm.xml file.

If students are mapped as a collection on Class, you can try using something like this:
var numberOfStudents = session.CreateCriteria<Class>()
.Add(Restrictions.IdEq(1))
.CreateCriteria("_students", "students")
.SetProjection(Projections.RowCount())
.UniqueResult<Int32>();
Where '1' is the id of the class (you can use other property) and '_students' is the name of the students collection.

Related

adding Columns based on received Objects to Table, via Spring

Working in Spring, and using H2 for now.
So, I have these two classes/Entities with their repositories working fine if I keep it simple.
Brand, which simply has a name and a price;
and VendingMachine, which has a model and some other properties;
BUT I need to implement this:
Different VendingMachines sell different Brands, for which they have a stock. The Brands it sells are determined when you instantiate the class.
So my idea is that the table for VendingMachines fields should look something like that:
MODEL////ID/////MAXCAPACITY////MAXBRANDS///BRAND01////BRAND02///BRAND03...
Where all the BRAND columns are the different Brands the machine sells, assigned at its instantiation as said before. The value will be an Integer.
My original idea was to send this data to the #Entity via a HashMap(Brand, Integer) but I don't know how to make it work once it tries to fit it into the JpaRepository, or even if it is possible this way.
Probably missing some magic Annotation I don't know.
Thanks, hope my question was well explained for everyone.
Extract Brand as new table/entity(read about database normalization).
And after just do one-to-many relationship with that entities(VendingMachines and Brand). would look like that:
class Brand
...
#ManyToOne
#JoinColumn(name="machine_id")
private VendingMachines machines;

The Specificity of Insertion Anomalies

I'm currently trying to understand the nuances of Insertion/Deletion/Modification anomalies in SQL.
Currently, the example I'm trying to understand is as follows:
ENROLLMENT
StudentID(PK) StudentName ClassID ClassName
111 Joe E1 English1
222 Bob E1 English1
333 Mary H1 History1
The problem the example wants me to answer is:
Which of the following causes an insertion anomaly?
with the answers being
Inserting a Student without a Class
and
Inserting a Class without a Student
I don't really understand why one of these answers is more right than the other, why, or how. It seems to me like either could be acceptable. Thanks in advance.
You need to think in terms of how data is added to a system naturalistically (i.e. what series of events occur in the real world).
In this case you would create a set of classes, prior to registration, and then create and assign students to them when they turned up to register.
You would be unlikely to create a set of students and then create and assign classes to each one.
A class might only be able to hold 30 students. How do you deal with any extra students who want to be registered for that class?
If you register 100 students and then decide to create classes, which subjects do you create?
Why do students decide to turn up to register? [Presumably because of the classes on offer.]
You can create as many classes as you're able to fit into your time-table. The number of students that actually register might mean a class is cancelled, but it has to exist in the first instance.
In summary, "Inserting a Student without a Class" would be more likely to cause an insertion anomaly.

Entity joining in xml

I have run into a little roadblock in regards to joining mantle entities. I would like to have a single depicting fields from two mantle entities, but am unsuccessful in joining them. Specifically, I have linked a list of party relationships (as contacts) to a single partyId (vendor), with the goal to make a vendor contacts page. However I am unable to link that form-list with the PartyContactMech and ContactMech entities (in order to display email and phone number in the same form-list). More generally, my question is how can one map lists to each other the same way one can map a list to a single object (using entity-find-one and value-field does not work when tried with entity-find)?
There is no need to make a view-entity (join entities) to do that. Simply do a query on the PartyRelationship entity in the main 'actions' part of your screen specifying the toParty (vendor). Then in your Form-List, use 'row-actions' to query the PartyContactMech and so on for each fromPartyId (contact) entry that the previous query returned. Also have a look at the PartyViewEntities file in Mantle USL. There are some helpful view-enties already defined for you there such as PartyToAndRelationship, PartyFromAndRelationship etc. Also note that entity-find-one returns a single "map" (value-field) as it queries on the PK. Whereas entity-find returns a list of maps (list). They are separate query types. If I understand your question correctly.

Remove object from Active Record Relation without deleting it

I'm working in Ruby on Rails 4 with Postgresql, and I've hit a bit of a snag. We have an Active Record model called AttendanceRecord which belongs to an AttendanceDay, AttendanceSwipe, Course, and CourseTimeSlot. Attendance Records were supposed to be unique on these fields, but something went wrong and duplicates snuck in. So, I wrote a method to find all of the Attendance Records which were duplicated and only keep one of them.
In the course of that method, I built an Active Record Relation of objects that shared the same attributes, like so:
records = AttendanceRecord.where(course_id: attributes[0], course_time_slot_id: attributes[1], attendance_swipe_id: attributes[2], attendance_day_id: attributes[3])
Nice relation, right?
Next, I found the object that I wanted to keep and named it to_keep. Then, I tried to remove just that object from the relation, like this:
records.delete(to_keep)
Unfortunately, I discovered that the delete method works a little differently on a Relation than it does on an Array. Instead of simply removing the object from the list, it actually does delete it from the database (without the callbacks).
So: I'm wondering if there is a method that I'm missing that will remove my to_keep object from the Relation without actually touching the object itself. Then, I'll be able to safely call records.destroy_all and happily go about my business. :)
If you want to exclude an object from a relation you can do so by id. For example:
records.where('id <> ?', to_keep.id).destroy_all
or, thanks to #trushkevich, in rails 4 you can do:
records.where.not(id: to_keep.id).destroy_all
This means that destroy_all will be called on the records you've identified already but excluding the to_keep record.

How to retrieve a specific object through a relationship in core data

Say I have 2 managed objects in my model: Department and Employee (as discussed in the Core Data Programming Guide). If I already have a specific department retrieved, I know I can get all employees in that department through
NSSet *departmentsEmployees = aDepartment.employees;
but what if I want to find a specific employee (e.g. with employeeId = 123) in that set, change one of its attributes, then save the change? How do I do that? Can I do a targeted query on the set? Or would I have to loop through each employee to find the one I want?
It seems that it would be better to try to find it in the employees NSSet instead of doing a whole new query to the entire data model because I already have a specific department.
Thank you
One way to narrow the search is to use -[NSSet filteredSetUsingPredicate:].