Fórum Persitencia com chave composta #10132

16/10/2009

0

@Entity
@Table(name = "produto")
public class Produto implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "COD_PRODUTO", nullable = false)
    private Integer codProduto;
    @Column(name = "DES_PRODUTO")
    private String desProduto;
    @Column(name = "PRECO_VENDA")
    private Double precoVenda;
    @Column(name = "PRECO_CUSTO")
    private Double precoCusto;
    @Column(name = "QTD_ESTOQUE")
    private String qtdEstoque;
    @JoinColumns({@JoinColumn(name = "COD_SUB_CATEGORIA",
  referencedColumnName = "COD_SUB_CATEGORIA"),
  @JoinColumn(name = "COD_CATEGORIA",
  referencedColumnName = "COD_CATEGORIA")})
    @ManyToOne
    private SubCategoria subCategoria;
   @Entity
@Table(name = "sub_categoria")
public class SubCategoria implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected SubCategoriaPK subCategoriaPK;
    @Column(name = "DESCRICAO")
    private String descricao;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "subCategoria")
    private Collection<Produto> produtoCollection;
    @JoinColumn(name = "COD_CATEGORIA", referencedColumnName = "COD_CATEGORIA", insertable = false, updatable = false)
    @ManyToOne
    private Categoria categoria;
@Entity
@Table(name = "categoria")
@NamedQueries({@NamedQuery(name = "Categoria.findByCodCategoria", query = "SELECT c FROM Categoria c WHERE c.codCategoria = :codCategoria"), @NamedQuery(name = "Categoria.findByDescricao", query = "SELECT c FROM Categoria c WHERE c.descricao = :descricao")})
public class Categoria implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "COD_CATEGORIA", nullable = false)
    private Integer codCategoria;
    @Column(name = "DESCRICAO")
    private String descricao;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "categoria")
    private Collection<SubCategoria> subCategoriaCollection;
Create table CATEGORIA (
 CATEGORIA_NO Int UNSIGNED NOT NULL AUTO_INCREMENT,
 DESCRICAO Varchar(100),
 Primary Key (CATEGORIA_NO)) ENGINE = InnoDB; Create table SUB_CATEGORIA (
 SUB_CATEGORIA_NO Int UNSIGNED NOT NULL,
 CATEGORIA_NO Int UNSIGNED NOT NULL,
 DESCRICAO Varchar(100),
 Primary Key (SUB_CATEGORIA_NO)) ENGINE = InnoDB; Create table PRODUTO (
 PRODUTO_NO Int UNSIGNED NOT NULL AUTO_INCREMENT,
 SUB_CATEGORIA_NO Int UNSIGNED NOT NULL,
 UNIDADE_NO Int UNSIGNED NOT NULL,
 DESCRICAO Varchar(100),
 VALOR_VENDA Double,
 VALOR_CUSTO Double,
 QUANTIDADE Int,
 CATEGORIA_NO Int UNSIGNED NOT NULL,
 Primary Key (PRODUTO_NO)) ENGINE = InnoDB;
Tenho a seguinte situação:  No formulário de produtos preciso selecionar uma categoria em um JComboBox
logo após esta seleção preciso que o JComboBox de subCategorias seja filtrado trazendo
somente as subCategorias relacionadas com a categoria selecionada anteriormente, no entanto
O netBeans gera os beans anotados da forma acima: No entanto desta forma somente é possivel selecionar uma subCategoria  e quado clico no botão salvar do formulário a categoria relacionada é automaticamente persistida
Existe um modo alternativo para mapear os objetos para fazer o que preciso?   Observe que na classe subCategoria existe uma chave primaria composta.   A apicação é desktop swing + jgoodies
Israel Barbosa

Israel Barbosa

Responder

Posts

19/10/2009

Dyego Carmo

Bom dia !

Voce precisa selecionar varias subcategorias para o produto ?

Valeu !


Responder

Gostei + 0

20/10/2009

Israel Barbosa

Não preciso selecionar somente uma subcategoria
Responder

Gostei + 0

21/10/2009

Dyego Carmo

Olá !

Desculpe , eu nao entendi BEM a sua duvida...

Voce não conseguindo fazer com que o Produto seja persistido com sua categoria ?

Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:  
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:  
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:          
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           -
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a seguinte
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a seguinte situação
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a seguinte situação:
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a seguinte situação: Categoria
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a seguinte situação: Categoria -
Responder

Gostei + 0

21/10/2009

Israel Barbosa

Vou tentar exemplificar:           - Imagine a seguinte situação: Categoria - Masculino,
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar