JPA cascade merge does not persist attribute - java

I've an entity on OpenJPA 2.0
#Entity
#Table(name = "os_wfentry")
#SequenceGenerator(name = "jwe_seq", sequenceName = "jwe_seq", initialValue = 10, allocationSize = 1)
public class JPAWorkflowEntry implements WorkflowEntry, Serializable {
private static final long serialVersionUID = -755511983025049452L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jwe_seq")
private long id;
#Column(name = "name")
private String workflowName;
#Column(name = "state")
private Integer workflowState;
#Column(name = "version")
private Integer version;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "entry")
private final List<JPACurrentStep> currentSteps;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "entry")
private final List<JPAHistoryStep> historySteps;
public JPAWorkflowEntry() {
currentSteps = new ArrayList<>();
historySteps = new ArrayList<>();
}
...
and on JPACurrent and JPAHistory step I've inserted:
#ManyToOne
#Column(name = "entry_id")
protected JPAWorkflowEntry entry;
It is all correct (in theory); but when I try to save (or update) a new instance of JPAWorkflowStore, having a NOT EMPTY list of (current or history) steps, list of steps attribute is not persistend on db and it always an empty list. Can You help me?? What am I doing wrong??

You need to specify #JoinColumn(name = "id", nullable = false) for your JPACurrent and JPAHistory.
What you have done is #Column(name = "entry_id") i dont see "entry_id" mapping to any cölumn in JPAWorkflowEntry

Related

My delete method does not delete in the #OneToOne relation

I have a 1 to 1 relation of Moneda and Remesa, but when I go to delete a Moneda or a Remesa, neither deletes me, I don't get any error, simply that they are not deleted in the database. The other relationships in my tables are working fine. I don't know if it's because I have something wrong with the #OneToOne relationship
Moneda.java
#Id
#SequenceGenerator(name = "moneda_sequence", sequenceName = "moneda_sequence", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "moneda_sequence")
#Column(name = "ID")
#JsonProperty("id")
private Long id;
#Column(name = "FECHA_INSERCION")
#JsonProperty("fechaInsercion")
#JsonDeserialize(using = JsonDateDeserializer.class)
#JsonSerialize(using = JsonDateSerializer.class)
private LocalDateTime fechaInsercion;
#Column(name = "FECHA_MODIFICACION")
#JsonProperty("fechaModificacion")
#JsonDeserialize(using = JsonDateDeserializer.class)
#JsonSerialize(using = JsonDateSerializer.class)
private LocalDateTime fechaModificacion;
#Column(name = "NOMBRE")
#JsonProperty("nombre")
private String nombre;
#Column(name = "ABREVIATURA")
#JsonProperty("abreviatura")
private String abreviatura;
#OneToOne
#JoinColumn(name = "pais", nullable = true)
#JsonProperty("pais")
private Pais pais;
#OneToMany(mappedBy = "moneda", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JsonIgnore
private Set<Corresponsable> corresponsables;
#OneToMany(mappedBy = "moneda", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JsonIgnore
private Set<Documento> documentos;
#OneToOne(mappedBy = "moneda", cascade = CascadeType.ALL, orphanRemoval = true)
#JsonIgnore
private Remesa remesa;
Remesa.java
#Id
#SequenceGenerator(name = "remesa_sequence", sequenceName = "remesa_sequence", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "remesa_sequence")
#Column(name = "ID")
private Long id;
#Column(name = "FECHA_INSERCION")
#JsonDeserialize(using = JsonDateDeserializer.class)
#JsonSerialize(using = JsonDateSerializer.class)
private LocalDateTime fechaInsercion;
#Column(name = "FECHA_MODIFICACION")
#JsonDeserialize(using = JsonDateDeserializer.class)
#JsonSerialize(using = JsonDateSerializer.class)
private LocalDateTime fechaModificacion;
private Integer tipoDoc;
private Integer entidad;
private Integer oficina;
private Integer referencia;
#OneToMany(mappedBy = "remesa", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JsonIgnore
private Set<Documento> documento;
#OneToOne(mappedBy = "remesa", cascade = CascadeType.ALL, orphanRemoval = true)
#JsonIgnore
private EnvioRemesa envio_id;
#OneToOne
#JoinColumn (name = "moneda_id", nullable = true)
private Moneda moneda;
My method to remove monedas:
public void getDeleteMoneda (Long moneda_id) {
try {
Moneda m = monedaRepo.findById(moneda_id).orElseThrow (() -> new Exception ("bad"));
if (m! = null) {
monedaRepo.deleteById (moneda_id); // HERE RETURN NULL DEBUGGING BUT THERE IS A MONEDA WITH THAT ID
}
} catch (Exception e) {
e.printStackTrace ();
}
}
The same thing happens with Remesa, the method is practically identical. Could it be a problem with the relationship of both tables?

Hibernate one to many mapping for multiple tables

#Entity
#Table(name="Visit")
public class Visit {
#Id
#XmlTransient
#JsonIgnore
#SequenceGenerator(name = "v_id_seq", sequenceName = "v_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "v_id_seq")
#Column(name = "id")
private Long id;
#OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
#OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;
#Entity
#Table(name="test")
public class Test {
#Id
#XmlTransient
#JsonIgnore
#SequenceGenerator(name = "t_id_seq", sequenceName = "t_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "t_id_seq")
#Column(name = "id")
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JsonBackReference
#JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit visit;
#Entity
#Table(name="direction")
public class directions {
#Id
#XmlTransient
#JsonIgnore
#SequenceGenerator(name = "d_id_seq", sequenceName = "d_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "d_id_seq")
#Column(name = "id")
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JsonBackReference
#JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit Visit;
Hello i am new to hibernate
I am trying to map OneToMany Visit-->Test and Visit-->direction but getting error
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property:
one visit can have multiple direction and test
how can i implement this?
plz help me!
The value of the mappedBy field on the #OneToMany annotation references java instance variable names, and it is case sensitive. You are setting it to Visit, but in the directions and test classes the variable names are visit.
The solution is to change property mappedBy from Visit to visit (lower case V):
#OneToMany(mappedBy = "visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
#OneToMany(mappedBy = "visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;

Mapping exception: Unable to find column with logical name

Ive having problems with my hibernate program. I have deleted and rewrited the class and still throws same error. I dont have declared the cod_modulo in this table, and even if i remove the one to many parameter, still throws same error. I dont understand anything.
Here the classes referenced. It also has getters and setters for all elements and the empty public constructor.
The modulo class
#Entity
#Table(name="modulo")
public class Modulo implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "SEQ_MODULO", sequenceName = "SEQ_MODULO", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MODULO")
private int cod_modulo;
#Column(name = "ancho_modulo")
private Double ancho_modulo;
#Column(name = "largo_modulo")
private Double largo_modulo;
#Column(name = "alto_modulo")
private Double alto_modulo;
#Column(name = "orden")
private Integer orden;
#ManyToOne(cascade = CascadeType.DETACH, targetEntity = Lineal.class, fetch = FetchType.EAGER)
#JoinColumn(name = "cod_lineal", referencedColumnName = "cod_lineal")
private Lineal lineal;
//#OrderBy
#JsonProperty(access = Access.WRITE_ONLY)
#OneToMany(mappedBy = "modulo", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Balda> baldas;
The lineal class
#Entity
#Table(name="lineal")
public class Lineal implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "SEQ_LINEAL", sequenceName = "SEQ_LINEAL", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_LINEAL")
private int cod_lineal;
#ManyToOne(cascade = CascadeType.DETACH, targetEntity = Seccion.class, fetch = FetchType.EAGER)
#JoinColumn(name = "cod_seccion", referencedColumnName = "cod_seccion")
private Seccion seccion;
#JsonProperty(access = Access.WRITE_ONLY)
#OneToMany(mappedBy = "lineal", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Modulo> modulos;
#Column(name = "refrigerado")
private Boolean refrigerado;
#Column(name = "horizontal")
private Boolean horizontal;
#Column(name = "pos_x")
private Double pos_x;
#Column(name = "pos_y")
private Double pos_y;
The error:
Mapping exception: Unable to find column with logical name: cod_modulo in org.hibernate.mapping.Table(lineal) and its related supertables and secondary tables
Try adding #Column for your cod_modulo property like so:
#Id
#SequenceGenerator(name = "SEQ_MODULO", sequenceName = "SEQ_MODULO", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MODULO")
#Column(name = "cod_modulo")
private int cod_modulo;

Set criteria on column in which data is saved as a string JSON

I have following entities:
public class **Product** implements Serializable {
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Id_Gen")
private Long id;
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#Fetch(value=FetchMode.JOIN)
#JoinColumn(name = "PRODUCT_DETAILS_TABLE")
private ProductJsonDetails productJsonDetails;
}
#Table(name = "PRODUCT_DETAILS_TABLE")
public class **ProductJsonDetails** implements Serializable {
#Id
#Access(AccessType.PROPERTY)
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Id_Gen")
private Long id;
#JsonIgnore
#OneToOne(fetch = FetchType.LAZY, mappedBy = "productJsonDetails")
private Product product;
#JsonIgnore
#Column(name = "**XYZ_JSON**")
private String **detailsJson**;
}
}
column XyZ_JSON has string in below format
{"name" : "ABC","type":{"id" : "LMN", type : "CCC"}}
I want to set criteria as below:
DetachedCriteria criteria = DetachedCriteria.forClass(Product.class, "product");
criteria.createAlias("product.productJsonDetails", "productJsonDetails", JoinType.LEFT_OUTER_JOIN);
critera.add (Restrictions.eq("productJsonDetails.type.id","1")) - >>> This throws error.
How to set restriction on Id element of String json ???

Hibernate ORA-02292: integrity constraint (ROOT.SYS_C007062) violated - child record found

I following have hibernate entities:
#Entity
#Table(name = "News")
public final class News implements Serializable, IEntity {
private static final long serialVersionUID = 3773281197317274020L;
#Id
#SequenceGenerator(name = "NEWS_SEQ_GEN", sequenceName = "NEWS_SEQ")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "NEWS_SEQ_GEN")
#Column(name = "NEWS_ID", precision = 0)
private Long newsId; // Primary key
#Column(name = "TITLE")
private String title;
#Column(name = "SHORT_TEXT")
private String shortText;
#Column(name = "FULL_TEXT")
private String fullText;
#Temporal(TemporalType.DATE)
#Column(name = "CREATION_DATE")
private Date creationDate;
#Temporal(TemporalType.DATE)
#Column(name = "MODIFICATION_DATE")
private Date modificationDate;
#OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
#JoinColumn(name = "NEWS_ID", updatable = false, referencedColumnName = "NEWS_ID")
#OrderBy("creationDate ASC")
private List<Comment> commentsList;
#ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinTable(name = "NEWS_TAG", joinColumns = { #JoinColumn(name = "NEWS_ID") }, inverseJoinColumns = { #JoinColumn(name = "TAG_ID") })
private Set<Tag> tagSet;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinTable(name = "NEWS_AUTHOR", joinColumns = { #JoinColumn(name = "NEWS_ID") }, inverseJoinColumns = { #JoinColumn(name = "AUTHOR_ID") })
private Set<Author> author;
And the second:
#SequenceGenerator(name = "COMMENTS_SEQ", sequenceName = "COMMENTS_SEQ")
#Entity
#Table(name = "Comments")
public class Comment implements Serializable, IEntity {
private static final long serialVersionUID = 3431305873409011465L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COMMENTS_SEQ")
#Column(name = "COMMENT_ID", precision = 0)
private Long commentId; // Primary key
#Column(name = "NEWS_ID")
private Long newsId;
#NotEmpty
#NotNull
#Column(name = "COMMENT_TEXT")
private String commentText;
#Temporal(TemporalType.DATE)
#Column(name = "CREATION_DATE")
private Date creationDate;
When I'm trying to remove entity News, I get the exception ORA-02292: integrity constraint (ROOT.SYS_C007062) violated - child record found. So, if I remove the property "updatable = false" it tries to set nullable fields into property Comment. What is my mistake? Please, help.
Thanks.
Because your news records have a one to one or one to many relation with comments. You most likely did not specifcy a CACASDE ON DELETE clause while defining your table. in order to delete entity NEWS you have to make sure that all of its related comments records are deleted or are referencing another NEWS record.
basicaly the definition of the ORA 02292 exception.

Categories

Resources