best way of add element to list in couchbase - couchbase

#Data
#NoArgsConstructor
#AllArgsConstructor
#Document
public class ClientMatrix {
#Id
private Long id;
#Field
private List<ClientMatrixPriceResultGroup> matrix; //1080 elements
#Field
private String request;
}
Two operations are there.
ClientMatrix object insert operation
add one by one matrix records. 1080 elements there.then 1080 operation
to perform 2nd operation I have to read ClientMatrix first. add ClientMatrixPriceResultGroup object to list matrix. And save again.
is that the best way ?

Related

Failed to lazily initialize a collection of role , Spring

I'm trying to get info from an API in Spring, but it gives me an error because one of the fields is a Set. How to get a json with all of the info? If i use JsonIgnore, it won't give me the set i need, right?
My class:
#Data
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "saloon")
public class Saloon implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
#OneToMany(mappedBy = "saloon")
private Set<Service> services;
...
Collection valued attributes are by default lazily-fetched. One easy solutio is to changing it to
#OneToMany(mappedBy = "saloon", fetch = FetchType.EAGER)
But that might cause severe performance issues in various cases.
To get to an optimal solution, you need to analyze your design, ie how you are using this property in your code.
You can take a look at OpenEntityManagerInViewInterceptor.

How can I make hibernate not override ID in object to be saved, and rather use its ID?

I'm trying to save a Foo object with ID=20. In my MySQL db the last row has Foo with ID=5. When I use the save function in the JpaRepository, it saves the object, but instead of using the ID I wanted it to have (20), it uses 6.
#Entity
#Data
#ToString
#NoArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
public class Foo implements Serializable {
#Id
private long id;
I have tried with #GeneratedValue(strategy = GenerationType.IDENTITY), but that doesn't work either. Is there a way to configure Hibernate not take the next sequential ID, but use what is stored in the object?

Remove Duplicate entry '59' for key 'PRIMARY in Hibernate

I am very new in Hibernate. I am using Hibernate with JPA. I have an annotated entity class and a table related to that entity class.
#Entity
public class Test implements Serializable {
#Id
#GenericGenerator(name="inc" , strategy="identity")
#GeneratedValue(generator="inc")
private int id;
private String address; // setter getter and constructor
}
When saving this entity, it insert the data into the db. But during application running process another application is inserting data into same table. when my application try to save the data then Duplicate entry '59' for key 'PRIMARY' exception generated. So I want to use a generator which can insert the data and generate id in database level rather than application level and the identifier must saved back to my entity.
Use the Table generator strategy or the sequence generator.
You do not have to specify a generator. You can use the default generator and never set the id manually. If the error still comes post your merge/persist method.
More informations about generators can you found here https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing
#Entity
public class Test implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String address; // setter getter and constructor
}

Pattern to map JPA Objects to DTO for Conversion to JSON

I have a somewhat philosophical question relating to mapping JPA Objects to JSON Strings. Of course there is no necessity for the source object to be a persistent object - it is just that that is my situation.
I have a collection of objects that are managed by Eclipse Link. I need to turn some of these objects into JSON Strings, however the mapping is not one-to-one. I am convinced that the conversion should be loosely coupled so as to isolate the JSON objects from changes in the underlying entities.
I am planning to have the JPA entity as such:
#Entity
#Table(name = "AbnormalFlags")
public class AbnormalFlag implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "Code", unique = false, nullable = false)
private String code;
#Column(name = "Description", unique = false, nullable = false)
private String description;
// Getters and setters
}
and the equivalent object to be converted to JSON
public class AbnormalFlagDTO implements java.io.Serializable {
private String code;
private String description;
private Boolean disabled;
// Getters and setters
}
Is there an elegant pattern or methodology I can use to facilitate this process for several types of objects.
Thanks in anticipation
My answer: no, and also you should generally extend DTOs with care (when reusing existing DTOs). But you could use a Map<String, Object> as a DTO (if you do not use the same DTO to read the data back). Besides you could create an APT (annotation processor tool) that generates the code for DTOs from your entities and then you simply modify them.
This is a perfect use case for Blaze-Persistence Entity Views as you will most probably also want to keep an eye on the performance of the query used for fetching the data.
I created the library to allow easy mapping between JPA models and custom interface defined models. The idea is that you define your target structure the way you like and map attributes(getters) via JPQL expressions to the entity model. Since the attribute name is used as default mapping, you mostly don't need explicit mappings as 80% of the use cases is to have DTOs that are a subset of the entity model.
A mapping for your model could look as simple as the following
#EntityView(AbnormalFlag.class)
interface AbnormalFlagDTO extends Serializable {
String getCode();
String getDescription();
Boolean getDisabled();
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
AbnormalFlagDTO dto = entityViewManager.find(entityManager, AbnormalFlagDTO.class, id);
The serialization of the entity view to JSON will work as expected. If you also want to deserialize objects, you will have to construct the object first and also add setters to the interface.

Simplest one-to-many Map case in Hibernate doesn't work in MySQL

I think this is pretty much the simplest case for mapping a Map (that is, an associative array) of entities.
#Entity
#AccessType("field")
class Member {
#Id
protected long id;
#OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
#MapKey(name = "name")
private Map<String, Preferences> preferences
= new HashMap<String, Preferences>();
}
#Entity
#AccessType("field")
class Preferences {
#ManyToOne Member member;
#Column String name;
#Column String value;
}
This looks like it should work, and it does, in HSQL. In MySQL, there are two problems:
First, it insists that there be a table called Members_Preferences, as if this were a many-to-many relationship.
Second, it just doesn't work: since it never populates Members_Preferences, it never retrieves the Preferences.
[My theory is, since I only use HSQL in memory-mode, it automatically creates Members_Preferences and never really has to retrieve the preferences map. In any case, either Hibernate has a huge bug in it or I'm doing something wrong.]
And of course, I sweat the problem for hours, post it here, and a minute later...
Anyway, the answer is the mappedBy element of the #OneToMany annotation:
#OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="member")
#MapKey(name = "name")
private Map<String, Preferences> preferences
= new HashMap<String, Preferences>();
Which makes a certain sense: which field in the Many entity points back to the One entity? Even allowing that looking for a matching #ManyToOne field was too error prone, I think that what they did do (assuming the existence of a mapping table) makes even worse.