1

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?

Erick
  • 823
  • 16
  • 37
  • It appears that you're new to interpreting exceptions in Java. The root cause of the whole problem is visible as the bottommost exception of the stack trace, the last one saying "Caused By". That part usually already represents the whole answer to the concrete problem. However, this information is completely missing in the question. Therefore we won't be able to translate the exception in layman's terms in case you're unable to interpret/research it. – BalusC Apr 14 '15 at 15:00
  • @BalusC ok lets forget about the exception. I didn't copy it all because I want the focus to be on the "good practice" part. Sorry about that – Erick Apr 14 '15 at 15:03
  • And now that I remembered correctly, not sure if I can post this type of "good practice" questions in SO. If I can't, please someone tell me and I can remove it. – Erick Apr 14 '15 at 15:06
  • 1
    That has been fleshed out several times before. This one is a good starting point: http://stackoverflow.com/questions/13011392/jsf-service-layer (also note the "See also" links in bottom of that answer). Is this acceptable as dupe? – BalusC Apr 14 '15 at 15:07
  • OK, I understand that it's acceptable as dupe. – BalusC Apr 14 '15 at 15:12

0 Answers0