-1
I have the following entity (decreased from the actual entity):
@Entity
@SequenceGenerator(name = "produtoSequence", sequenceName = "sequenceproduto")
public class Produto {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "produtoSequence")
private long id;
private String nome;
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = true)
private Date dataExclusao;
private String categoria;
//Getters e setters .....
Now you would need to include another entity, which will refer to a history. This history will basically store information about what happens to the product. A log in this case would not be enough, because I need to take some metrics from this history.
An example of history: a product has been deleted, a product exclusion history will be created, or a new product has been added, so a product inclusion history will be created.
I decided to use Nosql to store this information, especially Mongodb, because the data is saved in JSON, and do not have a pattern that I should follow (tables that appear in all queries, if I need a new data, I need to insert a new column). This is because the histories will sometimes have different data depending on the saved history.
Examples of Jsons that would represent the history.
History of product creation:
{
"id": 1,
"produto": {
"id": 15,
"nome": "Máquina de lavar XPTO2000",
"categoria": "Máquinas de lavar"
},
"dataCriacao": "11/06/2021",
"categoriaHistorico": "Criação de produto"
}
History of category change:
{
"id": 2,
"produto": {
"id": 15,
"nome": "Máquina de lavar XPTO2000",
"categoria": "Linha branca"
},
"categoriaAnterior": "Máquinas de lavar",
"categoriaNova": "Linha Branca",
"categoriaHistorico": "Alteração de categoria do produto"
}
This lack of pattern will help me a lot to develop, but how would you build a model for History that would communicate with the product model? If it were SQL to SQL it would be something like (below) using JoinColumn
and fetch, but handling SQL for Nosql, I haven’t found any notes that make this conversion or even an integration between the two databases.
public class Historico {
@Id
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "PRODUTO", referencedColumnName = "ID", foreignKey = @ForeignKey(name = "HISTORICO_PRODUTO_ID_FK"))
private Produto produto;
@Temporal(TemporalType.TIMESTAMP)
private Date dataCriacao = new Date();
//Getters e setters......
How could I save several "layouts" of history? I thought, in the backend, write an interface that contains the attributes that will be used in all history, and write implementations that have unique attributes, but I don’t know if there is such a possibility. Is that so wrong? Otherwise, I think of a gigantic class that would grow as new histories were created, which would be impossible to scale.