How to avoid a property/field without any annotation but with name "id" not mapped to '_id' field by Spring Mongo? - spring-data-mongodb

According to Spring Data Mongo docs
The following outlines what property will be mapped to the '_id'
document field:
A property or field annotated with #Id (org.springframework.data.annotation.Id) will be mapped to the '_id'
field.
A property or field without an annotation but named id will be mapped to the '_id' field.
I have some nested classes that have id field/property that does not need to mapped to mongo _id field but rather as a plain id field. Any tricks?

If you don't want that the id field is mapped to the mongo _id field you have to provide another field in your class with the#Id annotation. Then this field is used as mongo id and you can use the other in any way you want.
Example:
public class Foo {
#Id
private String mongoId;
// Your id
private String id;
}

Related

How to get a document from mongodb by using its string id and string collection name in SpringData

I know that generally, we need to do something similar to this for getting a document back from mongodb in spring data:
Define a class and annotate it with #Document:
#Document ("persons")
public class Person
Use MongoTemplete:
mongoOps.findById(p.getId(), Person.class);
The problem is that in runtime I don't know the class type of the document, I just have its string collection name and its string Id. How is it possible to retrieve the document using SpringData? Something like this:
db.myCollectionName.findOne({_id: myId})
The result object type is not a concern, it can be even an object, I just want to map it to a jackson JsonNode.
A possible workaround for this you can use the aggregate function of mongooperation like this
AggregationResults<Object> aggResults = mongoOps.aggregate(newAggregation(match(Criteria.where("_id").is(myId)) ,
myCollectionName, Object.class);
return aggResults.getUniqueMappedResult();

Dapper FluentMap: property with name 'Date' can't be mapped

I've faced the problem with Dapper FluentMap (version 1.60) which I don't know how to fix. It looks like Dapper can't map a column from a table if the corresponding property in .NET class has "Date" name.
I have the table in DB (I'm using MS SQL if that's matter) with column Dt of Date type.
There is an entity with DateTime property:
public class MyEntity {
public DateTime Date { get; set; }
}
and the corresponding mapping:
public class MyEntityMapper : EntityMap<MyEntity> {
public MyEntityMapper() {
Map(p => p.Date).ToColumn("Dt");
}
}
When I try to get data from the DB and map to MyEntity, I get the following error:
ArgumentNullException: Value cannot be null. Parameter name: meth
If I rename Date property in MyEntity to something else like Dt or JustDate - everything works fine. Is there such restriction in Dapper Fluent Map (not allowed to give a name to property equal to data type name in DB)?
If so, is it possible to overcome it somehow? Because in my case it's a bit problematic to rename property in MyEntity
Yes you are right, the only solution you can do was to change your entity property Date into other name.

getting Value of a field by its Name in apex salesforce

in my visualforce page i have some campaign object first user select an object then there is a multi picklist. in this picklist there is Label for all the fields user selects some fields then i have to show the value of these fields in the selected campaign object
for showing multiple picklist my apex function is
public List<SelectOption> getOptionalFields(){
Map <String, Schema.SObjectField> fieldMap= Campaign.sObjectType.getDescribe().fields.getMap();
List<SelectOption> fieldsName =new List<SelectOption>();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
fieldsName.add(new SelectOption(dfield.getName(),dfield.getLabel()));
}
but i have no idea how to show value for the the field
for exmple i have object instance like
Campaign c;
now i have to get value of any field whose Name is in string form.how to get corresponding value for that field.one solution is just write like
say
String fieldName;
and use multiple if
if(fieldName=='Name')
c.Name=
if(fieldName=='Id')
c.Id=
is there any other convenient method??please explain!!
You need to read about "dynamic apex". Every "concrete" sObject (like Account, Contact, custom objects) can be cast down to generic sObject (or you can use the methods directly).
Object o = c.get(fieldName);
String returnValue = String.valueOf(o);
There are some useful examples on dynamic get and set methods on Salesforce-dedicated site: https://salesforce.stackexchange.com/questions/8325/retrieving-value-using-dynamic-soql https://salesforce.stackexchange.com/questions/4193/update-a-records-using-generic-fields (second question is a bit more advanced)
You'll still need to somehow decide when to return it as String, when as number, when as date... Just experiment with it and either do some simple mapping or use describe methods to learn the actual field type...

filter properties with Objectify 4

I'm trying to filter with Objectify in GAE:
List<users> ul = ofy.load().type(Usuario.class).filter("name", "gildus").list();
In the User's class use anotation #Index:
#Entity
public class Users {
#Id
private Long id;
#Index
private String name;
...
The filter result is empty, although there is value "gildus". When I use the ID field if it shows results (....filter("id", "1").list() ).
What more could I do to make it work ?
When I use the ID field if it shows results (....filter("id", "1").list() ).
Don't use filter for id. Use the following instead:
Usuario user = ofy.load().type(Usuario.class).id(1).get();

Queries with Objectify: UmbrellaException

I am using Objectify to manage GAE Datastore for my GWT app. The problem is that I am not using queries properly and I get UmbrellaExceptions as per below:
Caused by: java.lang.RuntimeException: Server Error: java.lang.String cannot be cast to java.lang.Number
at com.google.web.bindery.requestfactory.shared.Receiver.onFailure(Receiver.java:44)
Say that I have a class Box with a unique field String id. I want to get the Box object whose id == "cHVQP6zZiUjM"
This is how I do it now:
public Box getBox(String boxId)
{
Objectify ofy = ObjectifyService.begin();
Query<Box> q=ofy.query(Box.class).filter("id",boxId);
Box targetBox = q.get();
return targetBox;
}
#Entity
public class Box extends DatastoreObject{
private String id;
private String title;
}
I tried doing this with ofy.load() but that method is not defined in my class Objectify (I don't know why).
Your key is encoded. Try using:
Box targetBox = ofy.get(Box.class, KeyFactory.stringToKey(boxId));
To decode your key.
The short answer: You are missing the #Id annotation in your entity.
The long answer: Id fields are special in the datastore. The id is not a real property, but rather a part of the Key that identifies the entity. You can't really filter on id fields, but you can filter on a special field called __key__. Objectify is somewhat clever about letting you filter by the id field and converting this to a __key__ filter under the covers, but it can't do it if you don't annotate the entity properly!
Actually I'm a little confused because Objectify shouldn't let you register the entity without an #Id field.
By the way, there are two sections of the documentation: Objectify4 (release coming soon) and Objectify3. Since you're using Ofy3, there is no load() method.
Another thing: Get-by-key operations are strongly preferred to queries when the operations are equivalent (as they are in your example).

Resources