In my project, class A references class B.
When saving an A using Jackson, I want to keep the reference to B, which is also saved.
To do that, I have a field called "id" in the referenced class and use this annotation:
#JsonIdentityInfo(scope = ReferencedClass.class, generator = ObjectIdGenerators.PropertyGenerator.class, property="id")
Doing this works fine when passing the id in the constructor, but I am creating an unknown number of ReferencedClasses.
Is there a way to make Jackson generate the id automatically?
And I wonder what the parameter generator = ObjectIdGenerators.PropertyGenerator.class means.
I did not find a solution that fit to this case, so in my main saveobject, I ended up using this code:
#JsonProperty private long generatedId = 0;
public long generateId() {
return generatedId++;
}
It works when using empty constructors and a proper #JsonTypeInfo if B is being extended.
I don't need generateId() anymore.
Using generator = ObjectIdGenerators.IntSequenceGenerator.class generated an id, which is only present in the JSON file.
Related
I know this is a very specific ask but I've got a situation where it would be very nice to have my custom class deserialized and have the collections parent ID set to a specific field. I know with the #DocumentId annotation we can do this for the documents own ID, is there some SDK method to extend this for my use case?
public class MyCustomClass {
#DocumentId public documentID;
...
#<insert magic here> public documentsCollectionsParentID
So for example, something like /users/<uid>/docs/<docid>, I already have the functionality for documentID to be set automatically to docID, but I'd like documentsCollectionsParentID set to uid. Is this in any way possible at the moment?
My current alternative is to deserialize, and the the object afterword:
MyCustomClass thing = (MyCustomClass)doc.toObject(MyCustomClass.class);
thing.setDocumentsCollectionsParentID(doc.getReference().getParent().getParent().getId())
Unfortunately, there is no method in Firebase that does that automatically. This is Java limitation and you are doing it correctly by deserializing the object.
I have a class that looks like this
#Data
#NodeEntity
public class StoryCharacter {
#Index(unique = true)
private String agnosticId;
private String name;
#Relationship(type = "FAMILIAR_WITH")
private Set<StoryCharacter> acquaintances;
}
I needed a custom ID that is not related to the default long id. So I introduced a field and set it as index.
But how to find the object by that id?
I wanted to do it like this
session.openSession().load(StoryCharacter.class, "custom_id")
but it fails with error that it must be Long. I assume that maybe I need to use Filter object for search by that id. Or is there another way?
If you want to use a custom id the field has to be annotated with #Id instead of #Index(unique=true). In cases you do not want to set the id manually, there is an option to provide a id generation strategy (more details in the documentation.
You are seeing this error because Neo4j-OGM cannot determine what type your id field has and falls back to the standard Long. If you define your id as mentioned above, the load will work.
Is there a way to generate a domain for a field that is defined as enum while generating DDL from the data model?
The default behaviour for fields that are defined as enum is either EnumType.STRING or EnumType.ORDINAL. In this particular case I use #Enumerated(EnumType.STRING).
MyEnum.java
public enum MyEnum {
MY_VALUE_1, MY_VALUE_2, MY_VALUE_3
}
MyEntity.java
#javax.persistence.Entity
public class MyEntity {
#javax.persistence.Id
private long id;
#javax.persistence.Enumerated(javax.persistence.EnumType.STRING)
private MyEnum status;
}
This is unfortunately defined as "only" character varying(255) (I am using PostgreSQL RDBMS).
ALTER TABLE myentity ADD COLUMN status character varying(255);
This of course gives an opportunity to put garbage there that has nothing to do with the actual enum.
INSERT INTO myentity(id, status) VALUES (1, 'THIS_HAS_NOTHING_TO_DO_WITH_THE_ENUM');
In this place I would like to be able to give a hint to Hibernate to generate a domain with only those values allowed that actually belong to the enumeration.
As a workaround I wanted to generate a check constraint using #Check annotation. This however regrettably cannot be done dynamically. I wanted to loop through the MyEnum.values() in order to generate this check so that I do not have to change it at the same time when the enum gets additional values.
public class MyEntity {
#javax.persistence.Id
private long id;
#javax.persistence.Enumerated(javax.persistence.EnumType.STRING)
#org.hibernate.annotations.Check(constraints = String.format("currentclub in %s", PlayerStatus.values()))
private MyEnum status;
}
This of course leads to a syntax error
The value for annotation attribute Check.constraints must be a constant expression.
which is clear to me as the attribute value must be determined at compilation time.
Is there any other way to do it in a clever way?
I have an instance of a class that looks as following
public class SomeEntity{
private OpMetric metric = Options.MEASURED;
private Scope scope = Scopes.GLOBAL;
}
Which need to be serialized into following XML
<SomeEntity xmlns="">
<op-metric>
<value>0.3</value>
</op-metric>
<calculated-scope>
<value>updated-global</value>
</calculated-scope>
</SomeEntity >
in both cases the value to be set in the xml is calculated based on enum values of the original fields ,meaning I need to use getters (+ #JsonIgnore on the fields ) and not just annotate the fields.
I've tried to use the following annotation on the getters to generate the format
#JacksonXmlProperty(isAttribute = false, localName = "value")
#JacksonXmlElementWrapper(localName="op-metric")
but it can only be used on one of them due to collision when using the same local name :
com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "value":
Using Mixins did not advance me much since obviously the same limitation applies there as well.
How should I go about creating this XML structure ?
I've ended up creating special methods for the purpose of XML creation ,each of which returns an instance of a class whose only field is named "value", which is then "automatically" gets serialized into the format required .
Annotations were added in using Jackson mixin
Morphia is a persistence library for the JVM, used when your app needs to communicate with a MongoDB. When you use it, apparently in Mongo/Morphia land, this unique ID needs to be a org.bson.types.ObjectId.
Is this true? Is there any reason why I can't use a Long for the ID, such as in:
// Groovy pseudo-code
#Entity
#JsonIgnoreProperties(ignoreUnknown=true)
#JsonInclude(JsonInclude.Include.NON_NULL)
class WidgetEntity {
#Id
Long id
// ...etc.
}
If it is possible to just use a Long, then what am I giving up by using a Long instead of an ObjectId?
You can use a Long but in this case you need to assign a value yourself.
From the morphia quickstart sample:
Also note that we had to add a new field "id" to our Hotel class. The
"id" value can be any persist-able type; like an int, uuid, or other
object. If you want an auto-generated value just declare it as an
ObjectId. If you don't use an ObjectId you must set the value before
saving.