JPA OneToOne cascade delete - jpa

i have a rellationship between 2 classes Document and Medecin
#Entity
public class Document implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String annee;
private Date dateVisite;
private String secteur;
private String typeVisite;
#OneToOne( fetch=FetchType.LAZY,cascade=CascadeType.REMOVE)
#JoinColumn(name = "idMedecin")
private Medecin medecin;
public Document(String annee,
Date dateVisite, String secteur, String typeVisite) {
super();
this.annee = annee;
this.dateVisite = dateVisite;
this.secteur = secteur;
this.typeVisite = typeVisite;
}
public String getSecteur() {
return secteur;
}
public void setSecteur(String secteur) {
this.secteur = secteur;
}
public String getTypeVisite() {
return typeVisite;
}
public void setTypeVisite(String typeVisite) {
this.typeVisite = typeVisite;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAnnee() {
return annee;
}
public void setAnnee(String annee) {
this.annee = annee;
}
public Date getDateVisite() {
return dateVisite;
}
public void setDateVisite(Date dateVisite) {
this.dateVisite = dateVisite;
}
}
and the medecin entity is
#Entity
public class Medecin implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String nom;
private String secteur;
private int telephone;
private int specialite;
public Medecin() {
super();
// TODO Auto-generated constructor stub
}
public Medecin(String nom, String secteur, int telephone, int specialite) {
super();
this.nom = nom;
this.secteur = secteur;
this.telephone = telephone;
this.specialite = specialite;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getSecteur() {
return secteur;
}
public void setSecteur(String secteur) {
this.secteur = secteur;
}
public int getTelephone() {
return telephone;
}
public void setTelephone(int telephone) {
this.telephone = telephone;
}
public int getSpecialite() {
return specialite;
}
public void setSpecialite(int specialite) {
this.specialite = specialite;
}
}
the problem is that after i generate the database i want if i delete the document record from the database i want the medecin record will be deleted also but in my case if i delete the document record the medecin record dont be deleted

Based on your configuration, Hibernate will generate Document table with foreign key pointing to Medicine table.
To achieve your requirement, it should be like:
public class Document {
#OneToOne(mappedBy = "document", cascade = CascadeType.REMOVE)
private Medicine medicine;
}
public class Medicine {
#OneToOne
private Document document;
}
Updated
public void delete(int id){
Document document = entityManager.find(Document.class, id);
entityManager.remove(document);
entityManager.flush();
}

Related

Spring Data JPA. Parent table data is not getting rolled back when exception occurred while inserting record in child table

I have 2 tables one to many relationship between Employee and Department table, Employee table are having column Id as PK, Name and Sal whereas Department table having column Dept_ID,Dept_Name & Dept_Loc and primary key is (Dept_ID,Dept_Name) i.e composite key and Dept_ID is foreign key ref from Employee table's Id column. The issue is when I save record in parent table i.e Employee it get saved but if in case I get exception while inserting record for child table i.e Department table,,data is not getting rolled back for EMployee table. Please help I m struggling and I am attaching my code.
public class GlEmployee implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
#Column(name = "EMP_ID")
private long empId;
#Column(name = "EMP_CITY")
private String empCity;
#Column(name = "EMP_NAME")
private String empName;
#Column(name = "EMP_SALARY")
private BigDecimal empSalary;
// bi-directional many-to-one association to EmpDepartment
#OneToMany(mappedBy = "glEmployee",cascade = CascadeType.ALL)
private List<EmpDepartment> empDepartments = new ArrayList<>();
public GlEmployee() {
}
public long getEmpId() {
return this.empId;
}
public void setEmpId(long empId) {
this.empId = empId;
}
public String getEmpCity() {
return this.empCity;
}
public void setEmpCity(String empCity) {
this.empCity = empCity;
}
public String getEmpName() {
return this.empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public BigDecimal getEmpSalary() {
return this.empSalary;
}
public void setEmpSalary(BigDecimal empSalary) {
this.empSalary = empSalary;
}
public List<EmpDepartment> getEmpDepartments() {
return this.empDepartments;
}
public void setEmpDepartments(List<EmpDepartment> empDepartments) {
this.empDepartments = empDepartments;
}
public EmpDepartment addEmpDepartment(EmpDepartment empDepartment) {
getEmpDepartments().add(empDepartment);
empDepartment.setGlEmployee(this);
return empDepartment;
}
public EmpDepartment removeEmpDepartment(EmpDepartment empDepartment) {
getEmpDepartments().remove(empDepartment);
empDepartment.setGlEmployee(null);
return empDepartment;
}
}
#Entity
#Table(name = "EMP_DEPARTMENT")
public class EmpDepartment implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private EmpDepartmentPK id;
#Column(name = "DEP_LOC")
private String depLoc;
public EmpDepartment(EmpDepartment id, String dep) {
}
// bi-directional many-to-one association to GlEmployee
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "DEP_ID", insertable = false, updatable = false)
private GlEmployee glEmployee;
public EmpDepartment() {
}
public EmpDepartmentPK getId() {
return this.id;
}
public void setId(GlEmployee glEmployee, String deptName) {
EmpDepartmentPK empDepartment = new
EmpDepartmentPK(glEmployee.getEmpId(), deptName);
this.id = empDepartment;
}
public String getDepLoc() {
return this.depLoc;
}
public void setDepLoc(String depLoc) {
this.depLoc = depLoc;
}
public GlEmployee getGlEmployee() {
return this.glEmployee;
}
public void setGlEmployee(GlEmployee glEmployee) {
this.glEmployee = glEmployee;
}
}
#Embeddable
public class EmpDepartmentPK implements Serializable {
// default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
#Column(name = "DEP_ID")
private long depId;
#Column(name = "DEP_NAME")
private String depName;
public EmpDepartmentPK() {
}
public EmpDepartmentPK(long depId, String depName) {
super();
this.depId = depId;
this.depName = depName;
}
public long getDepId() {
return this.depId;
}
public void setDepId(long depId) {
this.depId = depId;
}
public String getDepName() {
return this.depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
#Service
public class EmployeeService {
#Autowired
private EmployeeRepository employeeRepository;
#Transactional
public void createEmp() {
GlEmployee employee = new GlEmployee();
employee.setEmpCity("Pune");
employee.setEmpName("Ankush");
employee.setEmpSalary(new BigDecimal(200));
employeeRepository.save(employee);
EmpDepartment department = new EmpDepartment();
department.setId(employee, "ME");
department.setDepLoc(null);
department.setGlEmployee(employee);
employee.addEmpDepartment(department);
employeeRepository.save(employee);
}
}

New Entity is created with every Persist JPA

I just get started with JPA. I have an entity CustomerOrderEntity which has #OneToMany relation with another entity BeverageEntity. When I add the BeverageEntity to the CustomerOrderEntity a new entity is created of the type BeverageEntity and is persisted in the database as a new record. I do not want something like that I only need the CustomerOrderEntity to have the record of BeverageEntity and a new entity is not created for that inside the database.
These two entities are invoked from this class SalesManagement:
#Stateless
#Remote(SalesManagement.class)
public class SalesManagementBean implements SalesManagement {
#PersistenceContext(type = PersistenceContextType.TRANSACTION)
EntityManager em;
#Override
public void createOrder(CustomerOrder order) {
order.getOrderItems().forEach(item -> {
updateBeverageQuantity(item.getId(), item.getQuantity());
});
CustomerOrderEntity customerOrderEntity = new CustomerOrderEntity();
customerOrderEntity.setIssueDate(order.getIssueDate());
order.getOrderItems().forEach(item ->{
customerOrderEntity.addOrder(toEntity(item));
});
em.persist(customerOrderEntity);
}
public void updateBeverageQuantity(int b_id, int quantity) {
BeverageEntity beverageEntity = em.find(BeverageEntity.class, b_id);
if(beverageEntity != null) {
beverageEntity.setQuantity(beverageEntity.getQuantity() - quantity);
em.persist(beverageEntity);
}
}
public List<BeverageEntity> toBeverageEntity(List<Beverage> beverages) {
List<BeverageEntity> entities = new ArrayList<BeverageEntity>();
if(beverages != null && beverages.size() > 0) {
beverages.forEach(beverage -> {
BeverageEntity entity = new BeverageEntity();
entity.setName(beverage.getName());
entity.setManufacturer(beverage.getManufacturer());
entity.setQuantity(beverage.getQuantity());
entity.setPrice(beverage.getPrice());
entity.setIncentiveEntity(toIncentiveEntity(beverage.getIncentiveDTO()));
entities.add(entity);
});
return entities;
}
return null;
}
public IncentiveEntity toIncentiveEntity(IncentiveDTO incentiveDTO) {
if(incentiveDTO != null) {
IncentiveEntity entity = null;
return entity;
}
return null;
}
public BeverageEntity toEntity(Beverage b) {
BeverageEntity entity = new BeverageEntity();
entity.setName(b.getName());
entity.setManufacturer(b.getManufacturer());
entity.setQuantity(b.getQuantity());
entity.setPrice(b.getPrice());
entity.setIncentiveEntity(toIncentiveEntity(b.getIncentiveDTO()));
return entity;
}
}
I think the problem is in the method toBeverageEntity. But I am not sure about it.
The CustomerOrderEntity:
#Entity
public class CustomerOrderEntity {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
#Version
private int version;
private Date issueDate;
#OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private List<BeverageEntity> beverageEntities;
public void addOrder(BeverageEntity beverageEntity) {
this.beverageEntities.add(beverageEntity);
}
public List<BeverageEntity> getBeverageEntities() {
return beverageEntities;
}
public void setBeverageEntities(List<BeverageEntity> beverageEntities) {
this.beverageEntities = beverageEntities;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public Date getIssueDate() {
return issueDate;
}
public void setIssueDate(Date issueDate) {
this.issueDate = issueDate;
}
}
The BeverageEntity:
#Entity
public class BeverageEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String name;
private String manufacturer;
private int quantity;
private double price;
#Version
private int version;
#ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private IncentiveEntity incentiveEntity;
public BeverageEntity() {
}
public BeverageEntity(String name, String manufacturer, int quantity, Double price, IncentiveEntity entity) {
this.name = name;
this.manufacturer = manufacturer;
this.quantity = quantity;
this.price = price;
this.incentiveEntity = entity;
}
public BeverageEntity(String name, String manufacturer, int quantity, Double price) {
this.name = name;
this.manufacturer = manufacturer;
this.quantity = quantity;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public IncentiveEntity getIncentiveEntity() {
return incentiveEntity;
}
public void setIncentiveEntity(IncentiveEntity incentiveEntity) {
this.incentiveEntity = incentiveEntity;
}
}
Found the problem it was in the method toBeverageEntity as pointed out in the comments I was creating a new entity that was wrong. I needed to use find because the entity is already in the database.
BeverageEntity entity = em.find(BeverageEntity.class, beverage.getId());
entities.add(entity);

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.

Persisting a Joint Table in JPA

I have three entities, Trader, Portfolio and Member. Each Trader has a Portfolio and a Portfolio can have many Members. I have set up the following relationships. I'm not sure how to use the Jointable that is created, i.e. Portfolio_PORTFOLIOID and members_MEMBERID. Obviously I'd like to associate each portfolid with member id's, however I'm not sure how to go about this. How is the jointable data persisted?
My Portfolio class
#Entity
#Table(name="Portfolio")
#NamedQuery(
name="findPortfolioByTrader",
query="SELECT p FROM Portfolio p" +
" WHERE Trader = :trader"
)
public class Portfolio {
#Id
#GeneratedValue
private Integer portfolioId;
#Temporal(TIMESTAMP)
private Date lastUpdate;
private Integer balance;
private Trader trader;
private Collection<Member> members;
public Portfolio() {
this.lastUpdate = new Date();
}
public Portfolio(Integer balance, Trader trader) {
this.lastUpdate = new Date();
this.balance = balance;
this.trader = trader;
}
public Integer getPortfolioId() {
return portfolioId;
}
public void setPortfolioId(Integer portfolioId) {
this.portfolioId = portfolioId;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
#ManyToMany
#JoinTable(
name="MEMBER_PORTFOLIO",
joinColumns=
#JoinColumn(name="Member_MEMBERID", referencedColumnName="MEMBERID"),
inverseJoinColumns=
#JoinColumn(name="portfolio_PORTFOLIOID", referencedColumnName="PORTFOLIOID")
)
public Collection<Member> getMembers() {
return members;
}
public void setMembers(Collection<Member> members) {
this.members = members;
}
#OneToOne(cascade=ALL, mappedBy="portfolio")
public Trader getTrader()
{
return trader;
}
public void setTrader(Trader trader)
{
this.trader = trader;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
}
My Member class
#Entity
#Table(name="Member")
#NamedQuery(
name="findAllMembers",
query="SELECT m FROM Member m " +
"ORDER BY m.memberId"
)
public class Member implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -468520665316481235L;
private String memberId;
private String forename;
private String surname;
private Integer position;
private Integer majority;
private Integer IPO;
private Integer questions;
private Integer answers;
private Party party;
private Date lastUpdate;
private char status;
private Collection<Portfolio> portfolios;
private Collection<AskOrder> askOrders;
private Collection<BidOrder> bidOrders;
public Member() {
this.lastUpdate = new Date();
}
public Member(String memberId,String forename, String surname, Integer position,
Integer majority, Integer IPO, Integer questions, Integer answers, Party party) {
this.memberId = memberId;
this.forename = forename;
this.surname = surname;
this.position = position;
this.majority = majority;
this.IPO = IPO;
this.questions = questions;
this.answers = answers;
this.party = party;
this.lastUpdate = new Date();
this.askOrders = new ArrayList<AskOrder>();
this.bidOrders = new ArrayList<BidOrder>();
this.portfolios = new ArrayList<Portfolio>();
}
#Id
public String getMemberId() {
return memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public char getStatus() {
return status;
}
public void setStatus(char status) {
this.status = status;
}
#Temporal(TIMESTAMP)
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public String getForename()
{
return forename;
}
public void setForename(String forename)
{
this.forename = forename;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
public Integer getMajority() {
return majority;
}
public void setMajority(Integer majority) {
this.majority = majority;
}
public Integer getIPO() {
return IPO;
}
public void setIPO(Integer iPO) {
IPO = iPO;
}
public Integer getQuestions() {
return questions;
}
public void setQuestions(Integer questions) {
this.questions = questions;
}
public Integer getAnswers() {
return answers;
}
public void setAnswers(Integer answers) {
this.answers = answers;
}
#ManyToOne
public Party getParty() {
return party;
}
public void setParty(Party party) {
this.party = party;
}
#OneToMany(cascade=ALL, mappedBy="member")
public Collection<AskOrder> getAskOrders()
{
return askOrders;
}
public void setAskOrders(Collection<AskOrder> orders)
{
this.askOrders = orders;
}
#OneToMany(cascade=ALL, mappedBy="member")
public Collection<BidOrder> getBidOrders()
{
return bidOrders;
}
public void setBidOrders(Collection<BidOrder> bidOrders)
{
this.bidOrders = bidOrders;
}
#ManyToMany //FIXME should probably be many to many - done
public Collection<Portfolio> getPortfolios() {
return portfolios;
}
public void setPortfolios(Collection<Portfolio> portfolios) {
this.portfolios = portfolios;
}
}
#Entity
public class Portfolio
{
#Id
#GeneratedValue
private int id;
#ManyToMany
#JoinTable( name = "PortfolioMember",
#JoinColumns : #JoinColumn( name = "Portfolio_ID", referencedColumnName="id" ),
#InverseJoinColumns : #JoinColumn( name = "Member_ID", referencedColumnName="id" )
)
private List<Member> members;
}
#Entity
public class Member
{
#Id
#GeneratedValue
private int id;
#ManyToMany( mappedBy = members )
private List<Portfolio> portfolios;
}

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);
}