I have a situation to deliver solution for functionality of update one property of many objects. The point is that is possible to update any property. For example
class Book {
private String name;
private Long number;
private LocalDate date;
private SomeEnum name;
private boolean someFlag;
}
Possible is to update only one field at a time, but each field is available to update.
Of course I can create new filter class with each field and send it with given parameter I'd like to update, but I thinking about more elegant solution. More generic.
I have to List get objects by ids from database, update and save all with updated field.
What do You think this could be done. Is possible to create filter class with one generic field depend on what variable user give to update? What is the best practice to do something like that ? What is you experience ?
Related
I am uploading an object to a server and the "value" field accepts Strings, Ints, and Boolean values.
Here is the model with the clearly failed application of #SeralizedName.
public class InspectionFormItems {
#SerializedName("id")
private Integer id;
#SerializedName("type")
private String type;
#SerializedName("value")
private String stringValue;
#SerializedName("value")
private boolean boolValue;
#SerializedName("value")
private int intValue;
#SerializedName("name")
private String name;
#SerializedName("children")
ArrayList<ArrayList<InspectionFormItems>> subitems;
}
Most of the stackoverflow results I found were for Serializing entire objects, or De-Serializing. Some of my ideas were..
Is it possible to remove #SerializedName from the three value fields and only serialize the value that isn't null?
Can I build some sort of optional data-type-object in java that is set based on the data type that has a value?
This object is part of a larger object and the subitems object has a fair bit of depth to it it just as a note. I expect this to be a duplicate of some question, I just have been unable to find it so far.
Edit:
This is the closest question I have found though I would rather not custom serialize the entire object the way this person has as there could be 40-100 of these objects in this outgoing json
gson-same-field-name-different-types-serialize
I've also discovered I can not dynamically set the #SerializedValue attribute
is-it-possible-to-pass-method-parameter-to-annotation-on-a-method?
3rd Edit: Let me know if I should delete a bunch of this excess. I am trying to implement this method - inner serialization - this answer though is old an tough to make work right now.
Thanks to Andreas for their answer.
Using Object, I was able to avoid pre-defining the variable.
#SerializedName("value")
private Object value;
This allowed me to pass both String, Integer, or boolean to the same value field.
I am creating a web application that has a User object and each User object has an ArrayList of objects (Food) that need to be stored into a table in a mySQL database. I want to store the entire ArrayList into a single column. Would it be better to store it as a Json? Or should I just create a table for each User that stores the individual items of the ArrayList? The only problem I have is that the data would edited quite frequently.
EDIT: I have tables for both Users and Food. The idea is that Users add from the Foods from the Food table to their ArrayList and then I want to store that ArrayList in something.
public class User{
private int id;
private String username;
private List<Food> FoodList = new ArrayList<Food>();
}
public class Food{
private int id;
private String name;
private double protein;
private int calories;
...
}
As #ayrton mentions, creating (at least) two tables is a good idea (one for Users and one for Foods).
Definitely don't create a table per User
As #NeplatnyUdaj mentioned, a join table (where you map Foods to Users) might be warranted (or it could be overkill, depending on your needs).
Given the details you've described, JSON is almost certainly not the best approach (this isn't true in all situations, but if you were in such a situation, I'd probably suggest you consider a different type of database).
(Welcome to SO, btw!)
I get POJO that I get from database:
#Entity
public class Application implements Serializable{
private static final long serialVersionUID = 1L;
#Id
private Integer id;
#Column(name = "XMLOTHERINFORMATION", columnDefinition="XMLType")
private String xmlOtherInformation;
...
I want somehow create a getter for transient property for retrive unmarshalled class. I think that marshall/unmarshall in getter each time it has called is bad idea because xmlOtherInformation may be large and I am afraid it makes performance worse. Also there is another issue I String property and XMLClass should be synchronized before persistance. Any ideas how to deal with that?
To reach synchronization between two data structures I can recommend two options :
1) Use one data type (POJO preferable) in all your application and transform it to XML when needed. At this point you know that you transforming latest state of object.
2) Create a wrapper class that will contain both version of data and manage them separately. This will avoid transformation each time when you need some data type but also will decrease your performance and will allocate more memory.
Any way you have to decide what to do depending on your task. If you know that data going to be changed very frequently but not read then you may operate with single object and transform it in into another state when needed. In other case, if you have very frequently read operation then would be better to contain both data types in memory but then update will took more time.
I'm trying to integrate Hibernate Search into an application. The application entities can have multiple properties that are stored multilingual. This is accomplished by splitting the non multilingual and multilingual properties into seperate entities. An example snippet of this split looks like this (ommitted hibernate annotations as the database part is working fine):
#Indexed
public class Assignment {
#DocumentId
private UUID id;
#IndexedEmbedded
private Map<String, AssignmentI18n> i18n;
// Other properties
}
public class AssignmentI18n {
#DocumentId
#FieldBridge(impl = AssignmentI18nBridge.class)
private AssignmentI18nId id;
#Field
private String title;
#Field
private String description;
#Field
private String requirements;
public static class AssignmentI18nId {
private UUID assignmentId;
private String iso;
}
}
Now I would like to make this data searchable using Hibernate Search by treating it as a single entity in the index. The way the annotations are set up this happens however all entries of the multilingual fields are stored in the same field in the index. Basicly my index structure looks like this:
id
i18n.title
i18n.description
i18n.requirements
As all values of the multilingual data are indexed in the same field I can no longer distinguish what language they belong to. Is there a way to make the index look more like this?:
id
i18n.nl.title
i18n.en.title
i18n.nl.description
i18n.en.description
i18n.nl.requirements
i18n.en.requirements
Basicly I would like to add the HashMap key value to the index field name. I've looked into the possibiliy of treating the map as a field with a custom FieldBridge but that doesn't seem like the correct approach.
If you want to make the indexed fields look like the one you describe, use a custom field bridge. That's how you could get this structure, but since your map value is quite complex it would take quite a lot of custom code to create all fields.
You could create a feature request for Hibernate Search here. I could imagine that this type of feature would be of general use. Basically a way to either via an #IndexedEmbedded option or via an additional annotation define how the map key becomes part of the Lucene field name. That said, have you thought about how exactly you would then search in this index? Does the user somehow specify a locale and depending on this local you would target the appropriate fields? Also, how do you deal in your approach to configure different stemmers depending on the language type?
My use case is an index which holds titles of online media. The provider of the data associates a list of categories with each title. I am using SolrJ to populate the index via an annotated POJO class
e.g.
#Field("title")
private String title;
#Field("categories")
private List<Category> categoryList;
The associated POJO is
public class Category {
private Long id;
private String name;
...
}
My question has two parts:
a) is this possible via SolrJ - the docs only contain an example of #Field using a List of String, so I assume the serialization/marshalling only supports simple types ?
b) how would I set up the schema to hold this. I have a naive assumption I just need to set
multiValued=true on the required field & it will all work by magic.
I'm just starting to implement this so any response would be highly appreciated.
The answer is as you thought:
a) You have only simple types available. So you will have a List of the same type e.g. String. The point is you cant represent complex types inside the lucene document so you wont deserialize them as well.
b) The problem is what you are trying is to represent relational thinking in a "document store". That will probably work only to a certain point. If you want to represent categories inside a lucene document just use the string it is not necessary to store a id as well.
The only point to store an id as well is: if you want to do aside the search a lookup on a RDBMS. If you want to do this you need to make sure that the id and the category name is softlinked. This is not working for every 1:n relation. (Every 1:n relation where the n related table consists only of required fields is possible. If you have an optional field you need to put something like a filling emptyconstant in the field if possible).
However if these 1:n relations are not sparse its possible actually if you maintain the order in which you add fields to the document. So the case with the category relation can be probably represented if you dont sort the lists.
You may implement a method which returns this Category if you instantiate it with the values at position 0...n. So the solution would be if you want to have the first category it will be at position 0 of every list related to this category.