Child entity not auto generating id in mongo db - mongodb

When I try to persist the data in mongo db the child entity(departments) is not generating ObjectId.
The persisted data in mongodb, the departmentId auto generated id is not generated automatically
#org.springframework.data.mongodb.core.mapping.Document(collection="stores")
public class StoresEntity extends BaseEntityEntity implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#org.springframework.data.annotation.Id
private java.lang.String storeId;
#org.springframework.data.mongodb.core.mapping.Field(name ="store_name")
private java.lang.String storeName;
#org.springframework.data.mongodb.core.mapping.Field(name ="store_location")
private java.lang.String storeLocation;
private java.util.List<com.mt.mtamp.employees.entity.DepartmentsEntity> departments;
public java.lang.String getStoreId() {
return storeId;
}
public java.lang.String setStoreId(java.lang.String storeId) {
return this.storeId = storeId;
}
public java.lang.String getStoreName() {
return storeName;
}
public java.lang.String setStoreName(java.lang.String storeName) {
return this.storeName = storeName;
}
public java.lang.String getStoreLocation() {
return storeLocation;
}
public java.lang.String setStoreLocation(java.lang.String storeLocation) {
return this.storeLocation = storeLocation;
}
public java.util.List<com.mt.mtamp.employees.entity.DepartmentsEntity> getDepartments() {
return departments;
}
public java.util.List<com.mt.mtamp.employees.entity.DepartmentsEntity> setDepartments(java.util.List<com.mt.mtamp.employees.entity.DepartmentsEntity> departments) {
return this.departments = departments;
}
}
#org.springframework.data.mongodb.core.mapping.Document(collection="departments")
public class DepartmentsEntity extends BaseEntityEntity implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#org.springframework.data.annotation.Id
private java.lang.String departmentId;
#org.springframework.data.mongodb.core.mapping.Field(name ="department_name")
private java.lang.String departmentName;
public java.lang.String getDepartmentId() {
return departmentId;
}
public java.lang.String setDepartmentId(java.lang.String departmentId) {
return this.departmentId = departmentId;
}
public java.lang.String getDepartmentName() {
return departmentName;
}
public java.lang.String setDepartmentName(java.lang.String departmentName) {
return this.departmentName = departmentName;
}
}

Related

RHDM 7.9.0 Imported Data Object cannot be parsed

I have a java class that I want to import into RHDM 7.9.0. It imports but a warning message is displayed that the class cannot be parsed. Once imported I am able to view it in the Source view but when I try to open the Model View it shows the message "Java file could not be parsed, it won’t be possible to open the Editor tab, use the Source tab instead."
Is this normal or is there something wrong with my Java file?
package com.cdm_testing;
/**
* This class was automatically generated by the data modeler tool.
*/
public class Address implements java.io.Serializable {
static final long serialVersionUID = 1L;
private java.lang.String addressLine1;
private java.lang.String addressLine2;
private java.lang.String city;
private java.lang.String state;
private java.lang.String zip;
private java.lang.String county;
private java.lang.String country;
private java.lang.String FIPSCountyCode;
private java.lang.String FIPSStateCode;
private java.lang.Double latitude;
private java.lang.Double longitude;
private java.lang.String addressType;
private java.lang.Boolean validated;
public Address() {
}
public java.lang.String getAddressLine1() {
return this.addressLine1;
}
public void setAddressLine1(java.lang.String addressLine1) {
this.addressLine1 = addressLine1;
}
public java.lang.String getAddressLine2() {
return this.addressLine2;
}
public void setAddressLine2(java.lang.String addressLine2) {
this.addressLine2 = addressLine2;
}
public java.lang.String getCity() {
return this.city;
}
public void setCity(java.lang.String city) {
this.city = city;
}
public java.lang.String getState() {
return this.state;
}
public void setState(java.lang.String state) {
this.state = state;
}
public java.lang.String getZip() {
return this.zip;
}
public void setZip(java.lang.String zip) {
this.zip = zip;
}
public java.lang.String getCounty() {
return this.county;
}
public void setCounty(java.lang.String county) {
this.county = county;
}
public java.lang.String getCountry() {
return this.country;
}
public void setCountry(java.lang.String country) {
this.country = country;
}
public java.lang.String getFIPSCountyCode() {
return this.FIPSCountyCode;
}
public void setFIPSCountyCode(java.lang.String FIPSCountyCode) {
this.FIPSCountyCode = FIPSCountyCode;
}
public java.lang.String getFIPSStateCode() {
return this.FIPSStateCode;
}
public void setFIPSStateCode(java.lang.String FIPSStateCode) {
this.FIPSStateCode = FIPSStateCode;
}
public java.lang.Double getLatitude() {
return this.latitude;
}
public void setLatitude(java.lang.Double latitude) {
this.latitude = latitude;
}
public java.lang.Double getLongitude() {
return this.longitude;
}
public void setLongitude(java.lang.Double longitude) {
this.longitude = longitude;
}
public java.lang.String getAddressType() {
return this.addressType;
}
public void setAddressType(java.lang.String addressType) {
this.addressType = addressType;
}
public java.lang.Boolean getValidated() {
return this.validated;
}
public void setValidated(java.lang.Boolean validated) {
this.validated = validated;
}
public Address(java.lang.String addressLine1,
java.lang.String addressLine2, java.lang.String city,
java.lang.String state, java.lang.String zip,
java.lang.String county, java.lang.String country,
java.lang.String FIPSCountyCode, java.lang.String FIPSStateCode,
java.lang.Double latitude, java.lang.Double longitude,
java.lang.String addressType, java.lang.Boolean validated) {
this.addressLine1 = addressLine1;
this.addressLine2 = addressLine2;
this.city = city;
this.state = state;
this.zip = zip;
this.county = county;
this.country = country;
this.FIPSCountyCode = FIPSCountyCode;
this.FIPSStateCode = FIPSStateCode;
this.latitude = latitude;
this.longitude = longitude;
this.addressType = addressType;
this.validated = validated;
}
}
I found the answer to my question. I imported the class into the com.myspace package but the class definition had com.cdm_testing as the package. Once I changed the package in the Source view it became available in the Model view.

List not being saved

I'm trying to save a list of question options but its not being saved. Only the last row is being save.
Below is the code.
#Transactional
public void addQuestionOptions(QuestionOptionsRequest questionOptionsRequest, int questionId) {
List<QuestionOption> optionList = new ArrayList<>();
QuestionOption options = new QuestionOption();
Question question = questionRepository.findByQuestionId(questionId);
if(question != null) {
questionOptionsRequest.getQuestionOptions()
.stream()
.forEach(option -> {
options.setQuestionOption(option.getQuestionOption());
options.setQuestion(question);
options.setQuestionOptionNumber(option.getQuestionOptionNumber());
optionList.add(options);
});
questionOptionRepository.saveAll(optionList);
}
}
QuestionOption
#Entity
#JsonIgnoreProperties({"question"})
public class QuestionOption {
#Id
#GeneratedValue
private int questionOptionId;
private int questionOptionNumber;
private String questionOption;
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
#ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private Question question;
public void setQuestionOptionNumber(int questionOptionNumber)
{
this.questionOptionNumber = questionOptionNumber;
}
public void setQuestionOption(String questionOption)
{
this.questionOption = questionOption;
}
public String getQuestionOption()
{
return this.questionOption;
}
public int getQuestionOptionNumber()
{
return this.questionOptionNumber;
}
public int getQuestionOptionId() {
return questionOptionId;
}
public void setQuestionOptionId(int questionOptionId) {
this.questionOptionId = questionOptionId;
}
}
Question
#Entity
#Getter
#Setter
public class Question {
#Id
#GeneratedValue
private int questionId;
private int assessmentId;
private QuestionTypes questionType;
private String questionText;
private String questionURL;
private QuestionStatus questionStatus;
#OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="question")
private List<QuestionOption> questionOptions;
public void setQuestionId(int queId)
{
this.questionId = queId;
}
public void setQuestionText(String queTxt)
{
this.questionText = queTxt;
}
public void setQuestionType(QuestionTypes queType)
{
this.questionType = queType;
}
public void setQuestionURL(String queURL)
{
this.questionURL = queURL;
}
public int getQuestionId() {
return questionId;
}
public String getQuestionText() {
return questionText;
}
public QuestionStatus getQuestionStatus() {
return questionStatus;
}
public void setQuestionStatus(QuestionStatus questionStatus) {
this.questionStatus = questionStatus;
}
public QuestionTypes getQuestionTypes() {
return this.questionType;
}
public String getQuestionURL() {
return this.questionURL;
}
public int getAssessmentId() {
return this.assessmentId;
}
public void setAssessmentId(int assessmentId) {
this.assessmentId = assessmentId;
}
public QuestionTypes getQuestionType() {
return questionType;
}
}
You are creating only one object of QuestionOption
QuestionOption options = new QuestionOption();
Create this object inside for each.

update of an object containing a polymorphic set in MongoDB using SpringBoot

I have a "Fonction" class persisted in my Mongo data base which contain a set of objects extending "ItemAccesFonction" class :
#Document
public class Fonction {
#Id
private String _id;
#Indexed
private String name;
private Set<ItemAccesFonction> access;
public Fonction() {
}
public Fonction(String name, Set<ItemAccesFonction> acces) {
this.name = name;
this.access = acces;
this._id = name;
}
public Fonction(String name) {
this.name = name;
this.access = new HashSet<>();
this._id = name;
}
public String getId() {
return _id;
}
public void setId(String id) {
this._id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<ItemAccesFonction> getAccess() {
return access;
}
public void setAccess(Set<ItemAccesFonction> access) {
this.access = access;
}
}
Here is my "ItemAccesFonction" class and those which extend it :
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_class")
public class ItemAccesFonction {
private String _id;
private String _class;
public ItemAccesFonction() {
this._class = getClass().getCanonicalName();
}
public ItemAccesFonction(final String id) {
this._id = id;
this._class = getClass().getCanonicalName().toString();
}
public String getId() {
return _id;
}
public void setId(final String idItemAcces) {
this._id = idItemAcces;
}
public String get_class() {
return _class;
}
public void set_class(final String _class) {
this._class = _class;
}
}
public class ApplicationFonction extends ItemAccesFonction{
private List<String> listeGroupes;
public ApplicationFonction() {
super();
this.listeGroupes = new ArrayList<String>();
}
public ApplicationFonction(String id, List<String> listeGroupes) {
super(id);
this.listeGroupes = listeGroupes;
}
public List<String> getListeGroupes() {
return listeGroupes;
}
public void setListeGroupes(final List<String> listeGroupes) {
this.listeGroupes = listeGroupes;
}
}
public class RepertoireFonction extends ItemAccesFonction{
private String droitSelection;
public RepertoireFonction() {
super();
}
public RepertoireFonction(String id ,String droitSelection) {
super(id);
this.droitSelection = droitSelection;
}
public String getDroitSelection() {
return droitSelection;
}
public void setDroitSelection(final String droitSelection) {
this.droitSelection = droitSelection;
}
}
I encounter an issue when I try to update a Fonction. I get the following error, but not everytime :
{
"cause": {
"cause": {
"cause": null,
"message": "Target bean of type com.wps.gp.datamongo.model.acces.ItemAccesFonction is not of type of the persistent entity (com.wps.gp.datamongo.model.acces.RepertoireFonction)!"
},
"message": "Could not read payload!; nested exception is java.lang.IllegalArgumentException: Target bean of type com.wps.gp.datamongo.model.acces.ItemAccesFonction is not of type of the persistent entity (com.wps.gp.datamongo.model.acces.RepertoireFonction)!"
},
"message": "Could not read an object of type class com.wps.gp.datamongo.model.Fonction from the request!; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read payload!; nested exception is java.lang.IllegalArgumentException: Target bean of type com.wps.gp.datamongo.model.acces.ItemAccesFonction is not of type of the persistent entity (com.wps.gp.datamongo.model.acces.RepertoireFonction)!"
}
Does someone have an idea of what I am doing wrong?
Thanks

Delete a record using Spring data jpa

I have an entity 'Competence', this entity has OneToMany relation with two other entities : CandidatCompetence and OffreCompetence, and a ManyToOne relation with GroupCompetence.
And I have a rest delete service with will take the id of a Competence entity as following :
#Secured("ROLE_ADMIN")
#RequestMapping(value="/competences/{id}",method= RequestMethod.DELETE)
public void deleteCompetence(#PathVariable Long id) {
competenceMetier.deleteCompetence(id);
}
Then the deleteCompetence function will call a delete function from the Competence Repository which extends JpaRepository<Competence, Long> as following :
public void deleteCompetence(Long id) {
competenceRepository.delete(id);
}
The problem is that when I call the rest delete method, I get 200 as an http response, but nothing in the body, the same for the log I can't see the DELETE sql query anywhere, and the entity still exists in the database.
here are my entities :
Competence :
#Entity
public class Competence implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long codeCompetence;
private String titre;
private Boolean activated = true;
#OneToMany(mappedBy="competence",cascade = CascadeType.ALL)
private Collection<CandidatCompetence> candidatCompetences;
#OneToMany(mappedBy="competence",cascade = CascadeType.ALL)
private Collection<OffreCompetence> offreCompetences;
#ManyToOne
#JoinColumn(name = "groupCompetence")
private GroupCompetence groupCompetence;
public Long getCodeCompetence() {
return codeCompetence;
}
public void setCodeCompetence(Long codeCompetence) {
this.codeCompetence = codeCompetence;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
#JsonIgnore
#XmlTransient
public Collection<CandidatCompetence> getCandidatCompetences() {
return candidatCompetences;
}
#JsonSetter
public void setCandidatCompetences(Collection<CandidatCompetence> candidatCompetences) {
this.candidatCompetences = candidatCompetences;
}
#JsonIgnore
#XmlTransient
public Collection<OffreCompetence> getOffreCompetences() {
return offreCompetences;
}
public void setOffreCompetences(Collection<OffreCompetence> offreCompetences) {
this.offreCompetences = offreCompetences;
}
public Competence(String titre) {
super();
this.titre = titre;
}
public Competence() {
super();
// TODO Auto-generated constructor stub
}
#JsonIgnore
#XmlTransient
public GroupCompetence getGroupCompetence() {
return groupCompetence;
}
#JsonSetter
public void setGroupCompetence(GroupCompetence groupCompetence) {
this.groupCompetence = groupCompetence;
}
public Boolean getActivated() {
return activated;
}
public void setActivated(Boolean activated) {
this.activated = activated;
}
}
OffreCompetence :
#Entity
public class OffreCompetence implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long codeOffreCompetence;
private String niveauRequis;
#ManyToOne
#JoinColumn(name = "competence")
private Competence competence;
#ManyToOne
#JoinColumn(name="offre")
private Offre offre;
public Long getCodeOffreCompetence() {
return codeOffreCompetence;
}
public void setCodeOffreCompetence(Long codeOffreCompetence) {
this.codeOffreCompetence = codeOffreCompetence;
}
public String getNiveauRequis() {
return niveauRequis;
}
public void setNiveauRequis(String niveauRequis) {
this.niveauRequis = niveauRequis;
}
public Competence getCompetence() {
return competence;
}
public void setCompetence(Competence competence) {
this.competence = competence;
}
#JsonIgnore
public Offre getOffre() {
return offre;
}
#JsonSetter
public void setOffre(Offre offre) {
this.offre = offre;
}
public OffreCompetence(String niveauRequis) {
super();
this.niveauRequis = niveauRequis;
}
public OffreCompetence() {
super();
// TODO Auto-generated constructor stub
}
}
CandidatCompetence :
#Entity
public class CandidatCompetence implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long codeCandidatCompetence;
private String niveauExperience;
#ManyToOne
#JoinColumn(name = "candidat")
private Candidat candidat;
#ManyToOne
#JoinColumn(name = "competence")
private Competence competence;
public Long getCodeCandidatCompetence() {
return codeCandidatCompetence;
}
public void setCodeCandidatCompetence(Long codeCandidatCompetence) {
this.codeCandidatCompetence = codeCandidatCompetence;
}
public String getNiveauExperience() {
return niveauExperience;
}
public void setNiveauExperience(String niveauExperience) {
this.niveauExperience = niveauExperience;
}
public Candidat getCandidat() {
return candidat;
}
public void setCandidat(Candidat candidat) {
this.candidat = candidat;
}
public Competence getCompetence() {
return competence;
}
public void setCompetence(Competence competence) {
this.competence = competence;
}
public CandidatCompetence(String niveauExperience) {
super();
this.niveauExperience = niveauExperience;
}
public CandidatCompetence() {
super();
// TODO Auto-generated constructor stub
}
}
GroupCompetence :
#Entity
public class GroupCompetence implements Serializable {
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Long codeGroupCompetence;
private String titre;
private Boolean activated = true;
#OneToMany(mappedBy="groupCompetence",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Collection<Competence> competences;
public Long getCodeGroupCompetence() {
return codeGroupCompetence;
}
public void setCodeGroupCompetence(Long codeGroupCompetence) {
this.codeGroupCompetence = codeGroupCompetence;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public GroupCompetence(String titre) {
this.titre = titre;
}
public GroupCompetence() {
}
public Boolean getActivated() {
return activated;
}
public void setActivated(Boolean activated) {
this.activated = activated;
}
public Collection<Competence> getCompetences() {
return competences;
}
public void setCompetences(Collection<Competence> competences) {
this.competences = competences;
}
}
You should annotate your Service Method deleteCompetence with #Transactional.

JavaFX properties fail to persist

I'm using some JavaFX properties in my app:
#Entity(name = "Klanten")
#Table(name = "Klanten")
#NamedQueries({
#NamedQuery(name = "Klanten.findAll", query = "select k from Klanten k")
})
public class Klant implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int klantId;
#Transient
private final SimpleStringProperty naam = new SimpleStringProperty();
//private String naam;
//private String straat;
#Transient
private final SimpleStringProperty straat = new SimpleStringProperty();
private String telefoon;
private String huisnummer;
private String gsm;
private String woonplaats;
private String email;
private String postcode;
#OneToMany(mappedBy = "Klant", cascade = CascadeType.REMOVE)
private List<Raam> ramen;
public Klant() {
}
public Klant(String naam) {
this.naam.set(naam);
}
#Override
public String toString() {
return this.naam.get();
}
#Access(AccessType.PROPERTY)
#Column(name="naam")
public String getNaam() {
return this.naam.get();
}
public void setNaam(String naam){
this.naam.set(naam);
}
public List<Raam> getRamen() {
return this.ramen;
}
#Id
public int getKlantId() {
return klantId;
}
public void setKlantId(int klantId) {
this.klantId = klantId;
}
#Access(AccessType.PROPERTY)
#Column(name="straat")
public String getStraat() {
return straat.get();
}
public void setStraat(String straat) {
this.straat.set(straat);
}
public String getTelefoon() {
return telefoon;
}
public void setTelefoon(String telefoon) {
this.telefoon = telefoon;
}
public String getHuisnummer() {
return huisnummer;
}
public void setHuisnummer(String huisnummer) {
this.huisnummer = huisnummer;
}
public String getGsm() {
return gsm;
}
public void setGsm(String gsm) {
this.gsm = gsm;
}
public String getWoonplaats() {
return woonplaats;
}
public void setWoonplaats(String woonplaats) {
this.woonplaats = woonplaats;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public StringProperty naamProperty() {
return naam;
}
public StringProperty straatProperty() {
return straat;
}
}
However when I let JPA generate my database, the column "naam" and "straat" aren't generated. I get no error. How can I resolve this?
I tried all the things listed here:
Possible solution 1
Possible solution 2
These didn't work.
You can try to use regular properties and then have another get method which returns a new SimpleStringProperty, i.e.:
public StringProperty naamProperty() {
return new SimpleStringProperty(naam);
}
public StringProperty straatProperty() {
return new SimpleStringProperty(straat);
}