How can i get the reference objects in spring boot with mongodb - mongodb

When we use SQL with Spring boot, we can use hibernate and add #OneToMany relationships. It helps us to get the reference objects from another entity.
As an example, suppose Order and OrderDetails entities. When I query the Order entity, It automatically maps with the OrderDetails entity and it brings the whole document together. How can I do such a thing with Springboot+mongodb? Is there any easy way to solve this problem? Is it called ORM?

Related

Spring data JPA get child collection attributes using projections

I would like to fetch only few attributes from child collection entitites. So, is it possible using Spring JPA projections and Spring JPA repository?
Also, it should not execute multiple queries. Just one query to fetch the selected attributes form child collection.
I want to evaluate options first if it is available in Spring JPA before considering any other alternative.

Efficient querying when using DTOs in Breeze

We are using DTOs server side, and have configured a dbcontext using fluent api in order to give breeze the metadata it needs. We map 1:1 to our real database entities, and each DTO contains a simple subset of the real database entity properties.
This works great, but now I need to figure out a way to make queries efficient - i.e. if the Breeze client queries for a single item I don't want to have to create a whole set of DTO objects before I can filter. i.e. I want to figure out a way to execute the filter/sort on the actual entities, but still return DTO objects.
I guess I need to figure out a way to intercept the query execution in order to query my real database entities and return a DTO instead of the real database entity.
Any ideas for how to best approach this?
Turns out that if you use projection in a link statement, e.g.
From PossibleCustomer As Customer
In Customers
Select New CustomerDto With {.Id = PossibleCustomer.Id,
.Name = PossibleCustomer.Name,
.Email = PossibleCustomer.Email}
.. then linq is smart enough to still optimize any queries to the database - i.e. if I query on the linq statement above to filter for a single item by Id, the database is hit with that query for just a single item and a single DTO is created. Pretty clever stuff. This only works if you do a direct projection in the linq statement - if you call off to a function to create your DTO then this won't work.
Just in case others are facing the same scenario, you might want to look at AutoMapper - it can create these projections for you using a model you create just once - avoids all those huge linq statements that are hard to read and validate. The automapper projections (assuming you stick to the simple stuff) still allow the linq to entities magic that ensures you don't have to do table scans when you create your DTOs.

How do I create a read-only entity?

I have a web application using J2EE + Spring and a MySQL database. I need one entity which will be read-only. I have one main table with products, and they are only to read. There should be no insertion of new records and no updates currently.
The entity class should only read data and pass the entities forward (other entities are tables like order, shipments etc.).
Is there any solution for this? Does anyone have the same issue? Thanks for the help.
If you don't change an object, it will never be updated.
If you are using EclipseLink you can use the #ReadOnly annotation to mark something as read-only.

Entity Framework map multiple tables to one entity

I have a database with a table for active orders and one for inactive orders. I would like to model this in Entity Framework as one entity called Orders. I also need a way to determine if an order in this collection is active or not, preferably by having a status property on the entity that is set according to what table it is in. Is there anyway to do this using Entity Framework 1. What about in Entity Framework 4?
You could create a view in your DB and generate the entity from that.
Take a look at the Table Per Concrete Type inheritance.
It is described here in ADO.NET Team Blog.
I think this is what you are looking for: How to: Define a Model with Multiple Entity Sets per Type (Entity Framework)
"The Entity Data Model (EDM) allows an entity type to be included by multiple entity sets within a single entity container or for an entity type to be included in entity sets in multiple entity containers. Defining multiple entity sets per type (MEST) allows users to streamline their code when databases have partitioning or other such scenarios where multiple tables have the same structure."
If I am understanding you correctly both active and inactive orders would share the same properties (for example: both would have a decimal "amount" property) if this is the case then in EF 1, I am pretty certain this is not possible. I think you will have to fall back to Mapping your entities to a POCO Orders object.
A good way to do one entity that shares multiple tables is to use Entity Splitting. MSDN has a very simple tutorial that walks you through the process which is very easy, however, you may need to reshape your data model: http://msdn.microsoft.com/en-us/data/jj715646.aspx

How do I use JPQL to delete entries from a join table?

I have a JPA object which has a many-to-many relationship like this:
#Entity
public class Role {
//...
#ManyToMany(fetch=FetchType.EAGER)
#JoinTable(
name="RolePrivilege",
joinColumns=
#JoinColumn(name="role", referencedColumnName="ID"),
inverseJoinColumns=
#JoinColumn(name="privilege", referencedColumnName="ID")
)
private Set<Privilege> privs;
}
There isn't a JPA object for RolePrivilege, so I'm not sure how to write a JPQL query to delete entries from the privs field of a role object. For instance, I've tried this, but it doesn't work. It complains that Role.privs is not mapped.
DELETE FROM Role.privs p WHERE p.id=:privId
I'm not sure what else to try. I could of course just write a native query which deletes from the join table RolePrivilege. But, I'm worried that doing so would interact badly with locally cached objects which wouldn't be updated by the native query.
Is it even possible to write JPQL to remove entries from a join table like this? If not I can just load all the Role objects and remove entries from the privs collection of each one and then persist each role. But it seems silly to do that if a simple JPQL query will do it all at once.
The JPQL update and delete statements need to refer to an Entity name, not a table name, so I think you're out of luck with the approach you've suggested.
Depending on your JPA provider, you could delete the entries from the JoinTable using a simple raw SQL statement (should be 100% portable) and then programmatically interact with your cache provider API to evict the data. For instance in Hibernate you can call evict() to expire all "Role.privs" collections from the 2nd level cache:
sessionFactory.evictCollection("Role.privs", roleId); //evict a particular collection of privs
sessionFactory.evictCollection("Role.privs"); //evict all privs collections
Unfortunately I don't work with the JPA APIs enough to know for sure what exactly is supported.
I also was looking for a JPQL approach to delete a many-to-many relation (contained in a joining table). Obviously, there's no concept of tables in an ORM, so we can't use DELETE operations... But I wish there were a special operator for these cases. Something like LINK and UNLINK. Maybe a future feature?
Anyway, what I've been doing to achieve this need has been to work with the collections implemented in the entity beans, the ones which map the many-to-many relations.
For example, if I got a class Student which has a many-to-many relation with Courses:
#ManyToMany
private Collection <Courses> collCourses;
I build in the entity a getter and setter for that collection. Then, for example from an EJB, I retrieve the collection using the getter, add or remove the desired Course, and finally, use the setter to assign the new collection. And it's done. It works perfectly.
However my main worry is the performance. An ORM is supposed to keep a huge cache of all objects (if I'm not mistaken), but even using it to work faster, I wonder if retrieving all and every element from a collection is really effective...
Because for me it's as much inefficient as retrieving the registries from a table and post-filter them using pure Java instead of a query language which works direct or undirectly with the internal DB engine (SQL, JPQL...).
Java is an object-oriented language and the whole point of ORM is to hide details of join tables and such from you. Consequently even contemplating deletion from a join table without considering the consequences would be strange.
No you can't do it with JPQL, that is for entities.
Retrieve the entities at either end and clear out their collections. This will remove the join tables entries. Object-oriented.