How to make a menu in Hibernate + jsf + primefaces?

-1

I just edited the question. Well I already made the menu but without hibernate better said with jdbc. but at the time of deploying the project does not show me the menu with hibernate I will leave the tables.

I'm working with netbeans and when doing with hibernate, they generate the classes in a model package.

now in the dao.

public interface relacionDao {     public List listRelation (); }

in the daoImpl

public class relacionDaoImpl implements relacionDao {

@Override
public List<Relacion> listarRelacion() {
   List<Relacion> lista=null;
   Session session=HibernateUtil.getSessionFactory().openSession();
   Transaction t=session.beginTransaction();
    String hql="From Relacion";
    try{
        lista=session.createQuery(hql).list();
        t.commit();
        session.close();
    }catch(Exception e){
        t.rollback();
    }
    return lista;
}

on the bean.

@Named // @ ManagedBean @SessionScoped public class relacionBean implements Serializable {

private List<Relacion> lista;
private Relacion relacion = new Relacion();
private MenuModel model;
private relacionDaoImpl relaciondao = new relacionDaoImpl();

@PostConstruct
public void init() {
    this.listarMenus();
    model = new DefaultMenuModel();
    this.establecerPermisos();
}

public void listarMenus() {

    try {
        lista = relaciondao.listarRelacion();
    } catch (Exception e) {

    }
}

public MenuModel getModel() {
    return model;
}

public void setModel(MenuModel model) {
    this.model = model;
}

public void establecerPermisos() {
    Empleado lg = (Empleado) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("empleado");
    for (Relacion m : lista) {
        if (!m.getSubmenu().isProcesado()) {
            int actual = m.getSubmenu().getMenu().getIdMenu();

            if (m.getSubmenu().getMenu().getTipo().equals("S") && m.getEmpleado().getCodEmpleado() == lg.getCodEmpleado()) {
                DefaultSubMenu firstSubMenu = new DefaultSubMenu(m.getSubmenu().getMenu().getNomMenu());

                for (Relacion i : lista) {

                    if (actual == i.getSubmenu().getMenu().getIdMenu() && !i.getSubmenu().isProcesado()) {
                        Submenu submenu = i.getSubmenu();
                        Empleado empleado = i.getEmpleado();
                        if (submenu != null) {
                            if (submenu.getMenu().getIdMenu() == m.getSubmenu().getMenu().getIdMenu() && empleado.getCodEmpleado() == m.getEmpleado().getCodEmpleado()) {
                                DefaultMenuItem item = new DefaultMenuItem(i.getSubmenu().getNombreSubMenu());
                                item.setUrl(i.getSubmenu().getLinkSubMenu());
                                firstSubMenu.addElement(item);
                                i.getSubmenu().setProcesado(true);
                            }

                        }

                    }
                }
                model.addElement(firstSubMenu);
                m.getSubmenu().setProcesado(true);

            } else if (m.getSubmenu().getMenu().getIdMenu() != m.getSubmenu().getIdSubMenu() && m.getEmpleado().getCodEmpleado() == lg.getCodEmpleado()) {
                DefaultMenuItem item = new DefaultMenuItem(m.getSubmenu().getMenu().getNomMenu());
                item.setUrl(m.getSubmenu().getLinkSubMenu());
                model.addElement(item);
            }

        }
    }
}

in the xhtml template.

                                               Facelets Template     

<h:body>

    <div id="top" >
        <ui:insert name="top">
            <h:form>
                <p:menubar model="#{relacionBean.model}"/>


            </h:form>

        </ui:insert>
    </div>

    <div id="content" class="center_content">
        <ui:insert name="content">Content</ui:insert>
    </div>

    <div id="bottom">
        <ui:insert name="bottom">Bottom</ui:insert>
    </div>

</h:body>

making the display does not show me the menu. but when I do it with JDBC if it generates the menu I hope you excuse me but a little I do not understand hibernate thank you very much.

    
asked by Libra2880 24.09.2016 в 03:42
source

1 answer

1

You can not carry the code you have just put because we do not know how your entities are. However, basically Hibernate / JPA works like this.

  • Each class must be annotated with @Entity or be listed in the persistence unit ( persistence.xml ).
  • Every entity must have a field annotated with @Id , which represents the primary key.
  • Relationships between entities are made using @OneToOne , @OneToMany and @ManyToMany .
  • Entities must implement the Serializable interface.

Example

@Entity
public class Order implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id")
    private Long id;
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    private double amount;
    @OneToMany
    @JoinTable(
        name = "order_order_details",
        inverseJoinColumns=@JoinColumn(name = "order_detail_id"),
        joinColumns=@JoinColumn(name = "order_id")
    )
    private List<OrderDetail> details;

    // constructor, getters y setters
}

The entity OrderDetail contains the products and the quantity requested.

@Entity
public class OrderDetail implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_detail_id")
    private Long id;
    @OneToOne
    private Product product;
    private short quantity;

    // constructores, getters y setters
}

Entity Producto can be deducted.

Tip: In Hibernate / JPA there is a concept called fetch type or type of procurement , which indicates how we want to obtain relationships strong>. There are 2 types:

  • Eager (FetchType.EAGER): relations will be obtained together with the main entity. Let's suppose that you get an order, then at that same moment the list of details is brought along with it.
  • Lazy (FetchType.LAZY): Relationships are brought only when explicitly obtained by means of the getter.

If an order has many details, it is better to make that relationship late.

@OneToMany(fetch = FetchType.LAZY)

In your code I see a considerable amount of Joins making the code slightly legible. With Hibernate / JPA all this is done internally. To get an order with your detail, just call the getter, like this:

Order order = em.find(4359L, Order.class);
List<OrderDetail> details = order.getDetails();

// Imprime el producto y la cantidad de cada detalle 
details.stream().forEach(System.out::println);

Tip: You can see the queries created by Hibernate if you add the following lines to persistence.xml :

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />

As you can see, JPA / Hibernate abstracts you from many tasks with the database, like any ORM. However, not every project justifies using an ORM; The following must be taken into account:

  • High performance need
  • Complexity of the scheme

If we need high performance, it would be best to use a lightweight library such as jOOQ or JDBC flat. If our project is small, then it does not justify using an ORM.

    
answered by 24.09.2016 в 06:29