Carregar os dados para edição em java JSF 2.3 + java 8

0 respostas
jsf
Analistadesuporte_22

Boa tarde pessoa, gostaria de um suporte para resolver uma situação pontual.

Atualmente tenho duas tabelas, tabela de Tonner e tabela de Location, ao tentar realizar a edição da Location, não consigo carregar o Tonner associado a Location, sempre e exibido a lista dos Tonners, porem não o tonner que estava associado a Location.

Segue abaixo os códigos.

package entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import <a href="http://javax.persistence.Id">javax.persistence.Id</a>;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.SequenceGenerator;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity

@Table(name = location, schema = public)

@SequenceGenerator(name = locationSequenceGen, sequenceName = location_idlocation_seq, allocationSize = 1, initialValue = 1)

public class Location implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "locationSequenceGen")
@Column(name = "idLocation", nullable = false, unique = true)
private Integer idLocation;

@Column(name = "description", length = 90, nullable = false)
private String description;

@Column(name = "numberOfPages", nullable = false)
private Integer numberOfPages;

@Column(name = "qtd", nullable = false)
private Integer qtd;

@Temporal(TemporalType.DATE)
@Column(name = "dtRegister", nullable = false)
private Date dtRegister;

@Temporal(TemporalType.TIME)
@Column(name = "hrRegister", nullable = false)
private Date hrRegister;

@Column(name = "comeback", length = 3)
private String comeback = "NAO";

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idSector")
@Fetch(FetchMode.JOIN)
private Sector sector;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idTonner")
private Tonner tonner;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idUser")
private User user;

public Location() {
	// TODO Auto-generated constructor stub
}

public Location(Integer idLocation, String description, Integer numberOfPages, Integer qtd, Date dtRegister,
		Date hrRegister, String comeback, Sector sector, Tonner tonner, User user) {
	super();
	this.idLocation = idLocation;
	this.description = description;
	this.numberOfPages = numberOfPages;
	this.qtd = qtd;
	this.dtRegister = dtRegister;
	this.hrRegister = hrRegister;
	this.comeback = comeback;
	this.sector = sector;
	this.tonner = tonner;
	this.user = user;
}

public Integer getIdLocation() {
	return idLocation;
}

public void setIdLocation(Integer idLocation) {
	this.idLocation = idLocation;
}

public String getDescription() {
	return description;
}

public void setDescription(String description) {
	this.description = description;
}

public Integer getNumberOfPages() {
	return numberOfPages;
}

public void setNumberOfPages(Integer numberOfPages) {
	this.numberOfPages = numberOfPages;
}

public Integer getQtd() {
	return qtd;
}

public void setQtd(Integer qtd) {
	this.qtd = qtd;
}

public Date getDtRegister() {
	return dtRegister;
}

public void setDtRegister(Date dtRegister) {
	this.dtRegister = dtRegister;
}

public Date getHrRegister() {
	return hrRegister;
}

public void setHrRegister(Date hrRegister) {
	this.hrRegister = hrRegister;
}

public String getComeback() {
	return comeback;
}

public void setComeback(String comeback) {
	this.comeback = comeback;
}

public Sector getSector() {
	return sector;
}

public void setSector(Sector sector) {
	this.sector = sector;
}

public Tonner getTonner() {
	return tonner;
}

public void setTonner(Tonner tonner) {
	this.tonner = tonner;
}

public User getUser() {
	return user;
}

public void setUser(User user) {
	this.user = user;
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((idLocation == null) ? 0 : idLocation.hashCode());
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Location other = (Location) obj;
	if (idLocation == null) {
		if (other.idLocation != null)
			return false;
	} else if (!idLocation.equals(other.idLocation))
		return false;
	return true;
}

}

Tabela de Tonner

package entity;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;
import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import <a href="http://javax.persistence.Id">javax.persistence.Id</a>;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.OneToMany;

import javax.persistence.SequenceGenerator;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;
@Entity

@Table(name = tonner, schema = public)

@SequenceGenerator(name = tonnerSequenceGen, sequenceName = tonner_idtonner_seq, allocationSize = 1, initialValue = 1)

public class Tonner implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tonnerSequenceGen")
@Column(name = "idTonner", nullable = false, unique = true)
private Integer idTonner;

@Column(name = "description", length = 90, nullable = false)
private String description;

@Column(name = "model", length = 90, nullable = false)
private String model;

@Column(name = "qtd")
private Integer qtd = 0;

@Column(name = "qtdMinino")
private Integer qtdMinino = 0;

@Temporal(TemporalType.DATE)
@Column(name = "dtRegister", nullable = false)
private Date dtRegister;

@Temporal(TemporalType.TIME)
@Column(name = "hrRegister", nullable = false)
private Date hrRegister;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idColor")
private Color color;

@OneToMany(mappedBy = "tonner", fetch = FetchType.LAZY)
private List<Location> locations = new ArrayList<Location>();

public Tonner() {
	// TODO Auto-generated constructor stub
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((idTonner == null) ? 0 : idTonner.hashCode());
	return result;
}

public Tonner(Integer idTonner, String description, String model, Integer qtd, Integer qtdMinino, Date dtRegister,
		Date hrRegister, Color color, List<Location> locations) {
	super();
	this.idTonner = idTonner;
	this.description = description;
	this.model = model;
	this.qtd = qtd;
	this.qtdMinino = qtdMinino;
	this.dtRegister = dtRegister;
	this.hrRegister = hrRegister;
	this.color = color;
	this.locations = locations;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Tonner other = (Tonner) obj;
	if (idTonner == null) {
		if (other.idTonner != null)
			return false;
	} else if (!idTonner.equals(other.idTonner))
		return false;
	return true;
}

public Integer getIdTonner() {
	return idTonner;
}

public void setIdTonner(Integer idTonner) {
	this.idTonner = idTonner;
}

public String getDescription() {
	return description;
}

public void setDescription(String description) {
	this.description = description;
}

public String getModel() {
	return model;
}

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

public Date getDtRegister() {
	return dtRegister;
}

public void setDtRegister(Date dtRegister) {
	this.dtRegister = dtRegister;
}

public Date getHrRegister() {
	return hrRegister;
}

public void setHrRegister(Date hrRegister) {
	this.hrRegister = hrRegister;
}

public Color getColor() {
	return color;
}

public void setColor(Color color) {
	this.color = color;
}

public List<Location> getLocations() {
	return locations;
}

public void setLocations(List<Location> locations) {
	this.locations = locations;
}

public Integer getQtd() {
	return qtd;
}

public void setQtd(Integer qtd) {
	this.qtd = qtd;
}

public Integer getQtdMinino() {
	return qtdMinino;
}

public void setQtdMinino(Integer qtdMinino) {
	this.qtdMinino = qtdMinino;
}

}

O Método que carregar os dados para edição:
package control;

import java.io.IOException;

import java.io.Serializable;

import java.sql.Time;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;
import javax.annotation.PostConstruct;

import javax.enterprise.context.Conversation;

import javax.enterprise.context.ConversationScoped;

import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedProperty;

import javax.faces.context.FacesContext;

import javax.inject.Inject;

import javax.inject.Named;

import org.primefaces.event.RowEditEvent;

import dao.interfaces.ILocation;

import dao.interfaces.ISector;

import dao.interfaces.ITonner;

import dao.interfaces.IUser;

import entity.Location;

import entity.Sector;

import entity.Tonner;

import entity.User;

@Named
@ConversationScoped
public class LocationControl extends Generic implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Inject
private Conversation conversation;

private Location location;

@Inject
private ILocation locationDao;

private List<Location> listLocation;

private boolean update;

private Sector sector;

@Inject
private ISector sectorDao;

private List<Sector> listsector;

private Integer idSectors;

private Tonner tonner;

@Inject
private ITonner tonnerDao;

private List<Tonner> listTonners;

private Integer idTonner;

private User user;

@Inject
private IUser userDao;

private List<User> listUser;

@Inject
@ManagedProperty(value = "#{AutenticUser}")
private AutenticUser autenticUser;

@Override
@PostConstruct
public void init() {
	this.location = new Location();
	this.sector = new Sector();
	this.tonner = new Tonner();
	this.user = new User();
	locationDao.listLocation(location);
	tonnerDao.listTonner(tonner);
	sectorDao.listSector(sector);
	userDao.listUser(user);
	listsector = new ArrayList<>();
	listTonners = new ArrayList<>();
	listLocation = new ArrayList<>();
	listUser = new ArrayList<>();

}

public String beginConversation() {
	if (conversation.isTransient()) {
		conversation.begin();
	}
	return "/protect/pages/location/search.jsf?cid=" + conversation.getId();
}

private void endConversation() {
	if (!conversation.isTransient()) {
		conversation.end();
		return;
	}
}




@Override
public String actionEdit(Location location) {
	if (conversation.isTransient()) {
		conversation.begin();
	}
	this.location = location;
	listTonners.add(location.getTonner());
	update = true;
	return "changer?faces-redirect=true";
}


@Override
public List<Location> resultList() {
	return locationDao.listLocation(location);
}

public List<Sector> resultListSector() {
	return sectorDao.listSector(sector);
}

public List<Tonner> resultListTonner() {
	return tonnerDao.listTonner(tonner);
}

getter and setters omitidos-----

Data table com o botao de editar

<p:panel id=panelDataTable header=Lista de Locações>

<p:dataTable id=datatable var=location cellspacing=0"

cellpadding=2 paginatorPosition=bottom filterEvent=enter"

rowKey=#{location.idLocation}” editable=“true"

value=#{locationControl.resultList()}" selectionMode="single"

emptyMessage="Nenhum resultado encontrado!“

widgetVar=locationtable selection=#{locationControl.location}“

paginatorTemplate={CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink}

{LastPageLink} {RowsPerPageDropdown}"

rowsPerPageTemplate=10,40,60 paginator=true scrollable="true"

rows=10>
<p:column width="30" headerText="Tonner"
						filterMatchMode="contains" filterBy="#{location.tonner.model}"
						sortBy="#{location.tonner.model}">
						<f:facet name="header">
							<h:outputText value="Tonner" />
						</f:facet>
						<p:outputLabel value="#{location.tonner.model}" />
					</p:column>

				
					<p:column headerText="Editar" style="width:30px;">
						<p:commandButton icon="ui-icon-pencil"
							actionListener="#{locationControl.actionEdit(location)}"
							action="changer?faces-redirect=true">
						</p:commandButton>
					</p:column>

				</p:dataTable>
			</p:panel>

Segue a tela que recebe os dados para edição:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<ui:composition template="/protect/layout/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p=“http://primefaces.org/ui”>

<ui:define name="top">
	<ui:fragment rendered="#{autenticUser.administradorUsuario}">
		<h:form id="form">
			<br />
			<br />
			<p:toolbar>
				<p:toolbarGroup>
					<p:button value="Novo" style="background-color: #e6fffa"
						outcome="/protect/pages/location/insert.xhtml"
						icon="ui-icon-disk" />
					<p:button value="Pesquisa" style="background-color: #e6fffa"
						outcome="/protect/pages/location/search.xhtml"
						icon="ui-icon-arrowrefresh-1-w" />
				</p:toolbarGroup>
				<p:toolbarGroup align="right">
					<p:button value="Home" outcome="/protect/layout/template.xhtml"
						icon="ui-icon-home" />
				</p:toolbarGroup>
			</p:toolbar>
			<p:tabView effect="fade" effectDuration="normal">
				<p:tab id="personal" title="Locação de Tonner"
					titleStyle="background-color:#e0ebeb;">

					<h:panelGrid columns="2" styleClass="noBorders">

						<p:outputLabel value="Descrição :" for="desc" />
						<p:inputText id="desc" styleClass="up"
							value="#{locationControl.location.description}" maxlength="90"
							required="true" style="width:250px;"
							requiredMessage="Descrição: Campo obrigatório">
							<p:spacer />
							<p:message for="desc" />
						</p:inputText>

						<p:outputLabel value="Número de Paginas :" for="pages" />
						<p:inputText id="pages" styleClass="up"
							value="#{locationControl.location.numberOfPages}" maxlength="90"
							required="true" style="width:250px;"
							requiredMessage="Número de Paginas: Campo obrigatório">
							<p:spacer />
							<p:message for="pages" />
						</p:inputText>

						<p:outputLabel value="Qtd:" for="qtd" />
						<p:inputText id="qtd" styleClass="up"
							value="#{locationControl.location.qtd}" maxlength="90"
							required="true" style="width:250px;"
							requiredMessage="Qtd : Campo obrigatório">
							<p:spacer />
							<p:message for="qtd" />
						</p:inputText>

						<h:outputLabel value="Tonnner:" for="tonner" />
						<p:selectOneMenu id="tonner"
							style="width:500px; margin-top:13px;" styleClass="up"
							value="#{locationControl.idTonner}" required="true"
							requiredMessage="Tonner: Campo obrigatório." filter="true"
							filterText="Filter" filterMatchMode="startsWith">
							<f:selectItems value="#{locationControl.resultListTonner()}"
								var="tonner" itemValue="#{tonner.idTonner}"
								itemLabel="#{tonner.model} - Cor: #{tonner.color.color}" />
							<p:message for="tonner" />
						</p:selectOneMenu>


						<div align="right">
							<f:facet name="footer">
								<h:commandButton value="Salvar" ajax="false"
									styleClass="botao_salvar"
									action="#{locationControl.actionUpdate()}">
									<f:ajax execute="@form" render="@all" resetValues="true" />
								</h:commandButton>

								<h:commandButton value="Limpar" immediate="true" type="reset"
									styleClass="botao_limpar" />

								<h:commandButton value="Cancelar" immediate="true"
									styleClass="botao_cancelar"
									action="#{locationControl.actionCancel()}">
									<f:ajax execute="@form" render="@all" />
								</h:commandButton>
							</f:facet>
						</div>

					</h:panelGrid>
				</p:tab>
			</p:tabView>

		</h:form>
	</ui:fragment>
</ui:define>

</ui:composition>

Criado 6 de fevereiro de 2018
Respostas 0
Participantes 1