Bom dia colegas,
Sou novo no fórum, estou com problema em persistir tabela relacionada.
Meu banco de Dados:
Tabela (Produtos)
Id
Nome
codCategoria -> (Relacionamento de 1 para vários na tabela categoria)
Valor
Tabela (Categoria)
id
nome
Estou utilizando Netbeans 7.3 com MySQL 5.6.12.
Criei um formulário utilizando “Form de Amostra mestre / Detalhes”, inseri na classe produtos o JoinColumn e alterei INT para “Categoria”, para ler o nome da categoria conforme código abaixo:
[code] //Para exibir o relacionamento - trocar os int para Categoria(objeto)
@JoinColumn(name = “codCategoria”, referencedColumnName = “codCategoria”)
@ManyToOne
private Categoria codCategoria;
public Produtos(Integer id, String nome, Categoria codCategoria, String produtoCod, int codLocalProduto) {
this.id = id;
this.nome = nome;
this.codCategoria = codCategoria;
this.produtoCod = produtoCod;
this.codLocalProduto = codLocalProduto;
}
public Categoria getCodCategoria() {
return codCategoria;
}
public void setCodCategoria(Categoria codCategoria) {
Categoria oldCodCategoria = this.codCategoria;
this.codCategoria = codCategoria;
changeSupport.firePropertyChange(“codCategoria”, oldCodCategoria, codCategoria);
}
[/code]
Quando executo o netbeans exibe o seguinte erro:
[EL Info]: 2013-08-28 09:31:29.11--ServerSession(1485080714)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Info]: 2013-08-28 09:31:29.95--ServerSession(1485080714)--file:/F:/wrestaurante/build/classes/_wrestaurantePU login successful
[EL Warning]: 2013-08-28 09:31:30.073--UnitOfWork(91249071)--Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ConversionException
Exception in thread "AWT-EventQueue-0" Local Exception Stack:
Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [1], of class [class java.lang.Integer], from mapping [org.eclipse.persistence.mappings.DirectToFieldMapping[codCategoria-->carvao_lenha.produtos.CODCATEGORIA]] with descriptor [RelationalDescriptor(wrestaurante.bean.Produtos --> [DatabaseTable(carvao_lenha.produtos)])], could not be converted to [class [B].
Exception Description: The object [1], of class [class java.lang.Integer], from mapping [org.eclipse.persistence.mappings.DirectToFieldMapping[codCategoria-->carvao_lenha.produtos.CODCATEGORIA]] with descriptor [RelationalDescriptor(wrestaurante.bean.Produtos --> [DatabaseTable(carvao_lenha.produtos)])], could not be converted to [class [B].
at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:71)
at org.eclipse.persistence.internal.helper.ConversionManager.convertObjectToByteArray(ConversionManager.java:341)
at org.eclipse.persistence.internal.helper.ConversionManager.convertObject(ConversionManager.java:133)
at org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform.convertObject(DatasourcePlatform.java:160)
at org.eclipse.persistence.mappings.converters.SerializedObjectConverter.convertDataValueToObjectValue(SerializedObjectConverter.java:57)
at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.getAttributeValue(AbstractDirectMapping.java:699)
at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.valueFromRow(AbstractDirectMapping.java:1299)
at org.eclipse.persistence.mappings.DatabaseMapping.readFromRowIntoObject(DatabaseMapping.java:1326)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:445)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:798)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:715)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:668)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:601)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:560)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:717)
at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:769)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:433)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1081)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742)
at wrestaurante.view.produtosView.initComponents(produtosView.java:40)
at wrestaurante.view.produtosView.<init>(produtosView.java:22)
at wrestaurante.view.produtosView$1.run(produtosView.java:375)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
CONSTRUÍDO COM SUCESSO (tempo total: 32 segundos)
Código completo Produtos.java
package wrestaurante.bean;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
/**
*
* @author AntonioMarcosP
*/
@Entity
@Table(name = "produtos", catalog = "carvao_lenha", schema = "")
@NamedQueries({
@NamedQuery(name = "Produtos.findAll", query = "SELECT p FROM Produtos p"),
@NamedQuery(name = "Produtos.findById", query = "SELECT p FROM Produtos p WHERE p.id = :id"),
@NamedQuery(name = "Produtos.findByNome", query = "SELECT p FROM Produtos p WHERE p.nome = :nome"),
@NamedQuery(name = "Produtos.findByCodCategoria", query = "SELECT p FROM Produtos p WHERE p.codCategoria = :codCategoria"),
@NamedQuery(name = "Produtos.findByPrecoCusto", query = "SELECT p FROM Produtos p WHERE p.precoCusto = :precoCusto"),
@NamedQuery(name = "Produtos.findByPrecoVenda", query = "SELECT p FROM Produtos p WHERE p.precoVenda = :precoVenda"),
@NamedQuery(name = "Produtos.findByAnotacoes", query = "SELECT p FROM Produtos p WHERE p.anotacoes = :anotacoes"),
@NamedQuery(name = "Produtos.findByUltimaVenda", query = "SELECT p FROM Produtos p WHERE p.ultimaVenda = :ultimaVenda"),
@NamedQuery(name = "Produtos.findByProdutoCod", query = "SELECT p FROM Produtos p WHERE p.produtoCod = :produtoCod"),
@NamedQuery(name = "Produtos.findByCodLocalProduto", query = "SELECT p FROM Produtos p WHERE p.codLocalProduto = :codLocalProduto"),
@NamedQuery(name = "Produtos.findByEstatiscaProdutosPedidos", query = "SELECT p FROM Produtos p WHERE p.estatiscaProdutosPedidos = :estatiscaProdutosPedidos")})
public class Produtos implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "nome")
private String nome;
@Basic(optional = false)
//Para exibir o relacionamento - trocar os int para Categoria(objeto)
@JoinColumn(name = "codCategoria", referencedColumnName = "codCategoria")
@ManyToOne
private Categoria codCategoria;
@Column(name = "preco_custo")
private String precoCusto;
@Column(name = "preco_venda")
private String precoVenda;
@Column(name = "anotacoes")
private String anotacoes;
@Column(name = "ultima_venda")
@Temporal(TemporalType.TIMESTAMP)
private Date ultimaVenda;
@Basic(optional = false)
@Column(name = "produto_cod")
private String produtoCod;
@Basic(optional = false)
@Column(name = "codLocalProduto")
private int codLocalProduto;
@Column(name = "estatisca_produtos_pedidos")
private String estatiscaProdutosPedidos;
public Produtos() {
}
public Produtos(Integer id) {
this.id = id;
}
public Produtos(Integer id, String nome, Categoria codCategoria, String produtoCod, int codLocalProduto) {
this.id = id;
this.nome = nome;
this.codCategoria = codCategoria;
this.produtoCod = produtoCod;
this.codLocalProduto = codLocalProduto;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
Integer oldId = this.id;
this.id = id;
changeSupport.firePropertyChange("id", oldId, id);
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
String oldNome = this.nome;
this.nome = nome;
changeSupport.firePropertyChange("nome", oldNome, nome);
}
public Categoria getCodCategoria() {
return codCategoria;
}
public void setCodCategoria(Categoria codCategoria) {
Categoria oldCodCategoria = this.codCategoria;
this.codCategoria = codCategoria;
changeSupport.firePropertyChange("codCategoria", oldCodCategoria, codCategoria);
}
public String getPrecoCusto() {
return precoCusto;
}
public void setPrecoCusto(String precoCusto) {
String oldPrecoCusto = this.precoCusto;
this.precoCusto = precoCusto;
changeSupport.firePropertyChange("precoCusto", oldPrecoCusto, precoCusto);
}
public String getPrecoVenda() {
return precoVenda;
}
public void setPrecoVenda(String precoVenda) {
String oldPrecoVenda = this.precoVenda;
this.precoVenda = precoVenda;
changeSupport.firePropertyChange("precoVenda", oldPrecoVenda, precoVenda);
}
public String getAnotacoes() {
return anotacoes;
}
public void setAnotacoes(String anotacoes) {
String oldAnotacoes = this.anotacoes;
this.anotacoes = anotacoes;
changeSupport.firePropertyChange("anotacoes", oldAnotacoes, anotacoes);
}
public Date getUltimaVenda() {
return ultimaVenda;
}
public void setUltimaVenda(Date ultimaVenda) {
Date oldUltimaVenda = this.ultimaVenda;
this.ultimaVenda = ultimaVenda;
changeSupport.firePropertyChange("ultimaVenda", oldUltimaVenda, ultimaVenda);
}
public String getProdutoCod() {
return produtoCod;
}
public void setProdutoCod(String produtoCod) {
String oldProdutoCod = this.produtoCod;
this.produtoCod = produtoCod;
changeSupport.firePropertyChange("produtoCod", oldProdutoCod, produtoCod);
}
public int getCodLocalProduto() {
return codLocalProduto;
}
public void setCodLocalProduto(int codLocalProduto) {
int oldCodLocalProduto = this.codLocalProduto;
this.codLocalProduto = codLocalProduto;
changeSupport.firePropertyChange("codLocalProduto", oldCodLocalProduto, codLocalProduto);
}
public String getEstatiscaProdutosPedidos() {
return estatiscaProdutosPedidos;
}
public void setEstatiscaProdutosPedidos(String estatiscaProdutosPedidos) {
String oldEstatiscaProdutosPedidos = this.estatiscaProdutosPedidos;
this.estatiscaProdutosPedidos = estatiscaProdutosPedidos;
changeSupport.firePropertyChange("estatiscaProdutosPedidos", oldEstatiscaProdutosPedidos, estatiscaProdutosPedidos);
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Produtos)) {
return false;
}
Produtos other = (Produtos) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "wrestaurante.view.Produtos[ id=" + id + " ]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
Não estou conseguindo resolver o problema (could not be converted to [class [B]), por favor me ajudem.
Desde já agradeço
Abraço
Antonio