Spring Data MongoDB - model classes without annotations - spring-data-mongodb

This question is related to Spring Data MongoDB model classes without annotations.
I have a situation where I need to store my domain classes either in RDBMS store or NoSQL store. Say for example my domain classes are User, Feature, PaymentRequest, Order, OrderLine, OrderHeader etc.
I cannot use any annotation on my domain classes for various reasons.
Application team will specify in which persistent store they like to store. They might configure to store it in MongoDB or in MySQL or in Oracle etc.
My requirement is when I am storing in MongoDB say using spring-data-mongodb I want to leverage the DBRefs for associated objects in my domain object.
How can I achieve with spring-data-mongodb without using annotations in my model classes.
class Role
{
String id;
String roleName;
}
class User {
String id;
String firstName;
String lastName;
List<Role> userRoles;
}
When I save User object I want to ensure that in MongoDB Role objects are stored as DBRefs instead of actual Role object graph.
My question is ─ without using annotations in my User and Role classes ─ how can I achieve this?
I searched the user's forums and could not find a way. That's why I'm posting my question here.
Thanks,
Kishore Veleti A.V.K.

Not sure if you ever figured this out, but you can use AspectJ to create an ITD (inter-type declaration) to weave in the annotations into the class without having to actually modify the original code.
For example, to turn your userRoles into a DBRef, you just need this aspect:
import org.springframework.data.mongodb.core.mapping.DBRef;
privileged aspect User_Mongo {
declare #field: * User.userRoles : #DBRef;
}
This simply adds the #DBRef annotation to any fields within User named userRoles. You can look at the AspectJ documentation for more information on field patterns and ITDs.

Related

How can I modify the meta data of any field using apex

I am trying to change the metadata of fields of a object in salesforce using Apex. For example I am trying to make all required field non-required. I was able to retrieve all the required fields using the schema class and using methods like isNillable(). I wanted to ask if there is any way I can modify the metadata.
I have searched a lot regarding this but could not find any helpful results.
Schema.DescribeSObjectResult a_desc = objects.get(Name_of_of_object_whose_fields_are_to_be_retrieved).getDescribe();
Map<String, Schema.SObjectField> a_fields = a_desc.fields.getMap();
Set<string> x=a_fields.keySet();
//I am making a map of fieldname and bool(field required or not)
Map<String,boolean> result=new Map<String,boolean>();
for(String p:x)
result.put(p,a_fields.get(p).getDescribe().isCreateable() && !a_fields.get(p).getDescribe().isNillable() && !a_fields.get(p).getDescribe().isDefaultedOnCreate());
//what I want is to modify isNillable and other attributes and make these changes to the fields.
You can't make all required fields non-required because many of them are required at the database level and cannot be modified.
For example, the Name field (on any object that has a Name field) is always required. You cannot change this property. Likewise, Master-Detail relationship fields are always required, on standard and child objects.
To change the metadata of custom fields that are modifiable, you would have to use the Metadata API. It's not available in Apex, unless you use a wrapper like apex-mdapi. As a warning, modifying your org's metadata in a broad-based way via Apex is dangerous. You can cause damage to your org and its function in this way very easily. I strongly encourage you not to attempt to do this. Required fields are required for a reason.

Spring Data MongoDB default type for inheritance

My model consisted of the following example:
class Aggregate {
private SomeClassWithFields property;
}
Now I decided to introduce inheritance to SomeClassWithFields. This results in:
class Aggregate {
private AbstractBaseClass property;
}
The collection already contains a lot of documents. These documents do not contain a _class property inside the DB since they were stored before the inheritance was present.
Is there a way to tell Spring Data MongoDB to use SomeClassWithFields as the default implementation of AbstractBaseClass if no _class property is present?
The other solution would be to add the _class to all the existing documents with a script but this would take some time since we have a lot of documents.
I solved it by using an AbstractMongoEventListener
The AbstractMongoEventListener has an onAfterLoad method which I used to set the default _class value if none was present :) This method is called before any mapping from the DBObject to my domain model by spring so it works then.
Do note that I also needed to let spring data mongodb know the mappingBasePackage in order for it to be able to read an Aggregate before writing one. This can be done implementing the getMappingBasePackage method of the PreconfiguredAbstractMongoConfiguration class.

ASP.net Web API - Example pre-existing database Fluent NHibernate and Automapper

I am working through the ASP.net Web API 2 book (Git Hub)
I am trying to use Fluent NHibernate and Automapper to connect to a database. The book uses a fresh database while my database is pre-existing and not necessarily controlled by good practices.
Before joining tables etc. I would like to just be able to get a list of people and add a new person via the API. The only catch is that I would like to return less properties of the actual table and create a new person with even less than the model used to display a new person. I am having trouble understanding the flow of the automapper.
An example table might be
<pre>Person Entity
-person_id(int)
-person_name(varchar(100))
-person_location(int)
-person_phone(varchar(10))
-person_address(varchar(30))
</pre>
The model I want to use includes a subset of the items in the actual table. For example, maybe:
<pre>Person Model
-person_id(int)
-person_name(varchar(100)
-person_location(int)</pre>
There is also a newPerson model
<pre>NewPerson Model
-Name
-location</pre>
I have an Entity with all the person properties like
public virtual int person_id {get;set;}
but I have a model with the subset properties like
public long person_id {get; set;}
In the automapping configuration file I have a class NewPersonToPersonEntityAutoMapperTypeConfigurator and I have another class PersonEntityToPersonAutoMapperTypeConfigurator
I'm confused about how automapper is working. Should the AutoMapper file NewPersonToPersonEntityAutoMapperTypeConfigurator use something like
Mapper.CreateMap<NewPerson, PersonEntity>
.ForMember(opt => opt.person_id, x => x.Ignore())
...
.ForMember(opt => opt.person_address(varchar(30)))
While
PersonEntityToPersonAutoMapperTypeConfigurator uses something like
Mapper.CreateMap<PersonEntity, PersonModel>
Can anyone show me a good example of a simple scenario like this with automapper and a pre-existing table with extra unused properties or describe what Automapper should be doing or if I am on the right track?
Daniel - I think you're on the right track. Yes, you need an AutoMapper map for each "direction"... i.e. incoming service message to the EF entity, and from the EF entity to the service return message.
Your code to ignore certain properties is fine. You just need to make sure the entity is populated appropriately for the INSERT into the database. For example, the person_id column - is that required to be set? Or is that an auto-incrementing column??
To say it another way... you can certainly use AutoMapper (and our approach in the book) against an existing database. It's still just mapping properties from one type to another type.
Feel free to send some code my way.

Storing app preferences in Spring app

I'm working in a Spring MVC application that needs access to some variables that the admin user must set using a web wizard (smtp server, preferences, etc). I want to store this info in a database to be accessible by the app. Which is the best way to store this info?
Please spend some time with Section IV of Spring Reference Manual. There are plenty data persistence option supported by Spring. To name few popular ones: JDBC, JPA, Hibernate, XML
We use an approach with default values and a generic GUI. Therefore we use a property file that contains the default value as well as type information for every key. in the dayabdatabase we store only this values that have been modified by the user. The database schema is just a simple key value table. The key is the same like the one from the property file, the value is of type string, because we have to parse the default value anyway. The type info (int, positiveInt, boolean, string, text, html) from the propty file is used by the generic GUI to have the right input for every key.
Example:
default.properties
my.example.value=1
my.example.type=into
default.properties_en
my.example.title=Example Value
my.example.descruption=This is..
Db:
Key=string(256)
Value=string(2048)

Create lists of multiple users as a model property in Google App Engine

I would like to create a Group model in Google App Engine and then have an attribute where I can create a list of UserReferences. The documentation said:
"A property can have multiple values, represented in the datastore API as a Python list. The list can contain values of any of the value types supported by the datastore."
Would I implement this by creating:
class Group(db.Model):
group_list = db.ListProperty(users.User)
Or might I be better served by simply listing the user entity keys?
http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html
keys are better placed in ReferenceProperty and their purpose is to create relationships between two kinds.
You can simply create the listproperty and as your list grows keep adding listitems to it.
class Group(db.Model):
group_list = db.ListProperty()
This depends on your use-case. If you already have a User model, to store additional data about your users, then using a db.ListProperty(Key) for User model keys is probably your best option.

Resources