So lets say I have a JSF managed bean called Company. This bean is also a JPA entity class. It looks like this:
package com.microtekcomputers.models;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import com.microtekcomputers.services.DaoCompany;
@Entity
@ManagedBean
@ViewScoped
public class Company implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int company_id;
@ManagedProperty("#{companyService}")
private DaoCompany daoCompany;
@Column
private String name;
private String address1;
private String address2;
private String city;
private String state;
private String zipcode;
private String contact;
private String tel1;
private String tel2;
private String fax;
private String comments;
//collection of networks, ISPs, software, hardware
@OneToMany(mappedBy="company")
private Set<Network> networks = new HashSet<Network>();
@OneToMany(mappedBy="company")
private Set<ISP> isps = new HashSet<ISP>();
@OneToMany(mappedBy="company")
private Set<Software> softwares = new HashSet<Software>();
@OneToMany(mappedBy="company")
private Set<Hardware> hardwares = new HashSet<Hardware>();
/* I deleted the other getters and setters (but they go here) */
//I NEED THE SETTER FOR MY MANAGED PROPERTY
public void setDaoCompany(DaoCompany daoCompany) {
this.daoCompany = daoCompany;
}
//dao methods
public void insertCompany()
{
daoCompany = new DaoCompany();
daoCompany.insertCompany(this);
}
}
The idea is to provide the user a form to write company data (name, address, city, etc) and on the submit button, I would call the insertCompany(). I have a ManagedProperty for my DaoCompany class. Here is the code:
package com.microtekcomputers.services;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.microtekcomputers.models.Company;
@ManagedBean(name="companyService")
@ApplicationScoped
public class DaoCompany {
//I created a singleton class for my Session Factory
private SessionFactory sf = SingletonSessionFactory.getSessionFactory();
private Session session = sf.openSession();
public void insertCompany(Company company)
{
Transaction t = session.beginTransaction();
session.save(company);
t.commit();
session.flush();
session.close();
}
public void updateCompany(Company company)
{
Transaction t = session.beginTransaction();
session.update(company);
t.commit();
session.flush();
session.close();
}
}
My question is if it is good practice to use both JSF managed bean and JPA entity on the same class. If not, what could be my alternative? Because don't I need the insertCompany() on my bean class to be able to access it on the xhtml?