@OneToMany Bidirectional

0

Good afternoon, folks. I'm developing a financial web application and I'm having trouble making the relationship between Lancamento and ItemLancamento . I'll post what I've done so far, this is giving error when adding item.

CLASS Lacing:

@SuppressWarnings("serial")
@Entity
@Table(name = "LANCAMENTO")
public class Lancamento extends GenericModel {

@Column(name = "DESCRICAO", nullable = false, length = 100)
private String descricao;

@Column(name = "DATA_LANCADA", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date dataCriacao;

@Column(name = "VALOR_LANCADO", nullable = false, precision = 7, scale = 2)
private BigDecimal valorLancamento;

@Column(name = "FIXO", nullable = false)
private Boolean fixo;

@ManyToOne(optional = false, cascade = {CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "ID_CATEGORIA", foreignKey = @ForeignKey(name = "LANCA_CATEGORIA_FK"), nullable = false)
private Categoria categoria;

@ManyToOne(optional = false, cascade = {CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "ID_CONTA", foreignKey = @ForeignKey(name = "LANCA_CONTA_FK"), nullable = false)
private Conta conta;

@ManyToOne(optional = false, cascade = {CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "ID_USUARIO", foreignKey = @ForeignKey(name = "LANCA_USER_FK"))
private Usuario usuario;

@OneToMany(mappedBy = "lancamento", cascade = {CascadeType.ALL, CascadeType.REFRESH}, orphanRemoval = true)
private List<ItemLancamento> itens;
   public void adicionaItensL(ItemLancamento item){
    this.itens.add(item);
    }

CLASS ItemLancement:

@SuppressWarnings("serial")
@Entity
@Table(name = "ITEM_LANCAMENTO")
public class ItemLancamento extends GenericModel {

@Column(name = "DATA", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date data;

@Column(name = "VALOR", nullable = false, precision = 7, scale = 2)
private BigDecimal valor;

@ManyToOne(optional = false, cascade = {CascadeType.ALL, CascadeType.REFRESH})
@JoinColumn(name = "ID_LANCA", foreignKey = @ForeignKey(name = "ITEM_LANCA_FK"))
private Lancamento lancamento;

CLASS main ()

        @Test
        public void salvar() throws ParseException {
        try {

        CategoriaDAO categoriaDAO = new CategoriaDAO();
        Categoria categoria = categoriaDAO.buscarPorId(19L);


        ContaDAO contaDAO = new ContaDAO();
        Conta conta = contaDAO.buscarPorId(7L);


        UsuarioDAO usuarioDAO = new UsuarioDAO();
        Usuario usuario = usuarioDAO.buscarPorId(15L);


        ItemLancamento item = new ItemLancamento();
        item.setData(new SimpleDateFormat("dd/MM/yyyy").parse("01/04/2017"));
        item.setValor(new BigDecimal("22.15"));

        if (categoria == null && conta == null || usuario == null && item == null){
            System.out.println("ASSOCIAÇÕES DE LANÇAMENTO NULA!!");
        } else {
            Lancamento lancamento = new Lancamento();
            LancamentoDAO lancamentoDAO = new LancamentoDAO();

            lancamento.setDescricao("Testando meu primeiro lançamento");
            lancamento.setFixo(true);
            lancamento.setDataCriacao(new SimpleDateFormat("dd/MM/yyyy").parse("14/04/2017"));
            lancamento.setValorLancamento(new BigDecimal("77.56"));


            lancamento.setConta(conta);
            lancamento.setUsuario(usuario);
            lancamento.setCategoria(categoria);

            [![inserir a descrição da imagem aqui][1]][1]//PROBLEMA É AQUI.. ACHO
            lancamento.adicionaItensL(item);

            System.out.println("AQUI SALVA 9");

            item.setLancamento(lancamento);
            System.out.println("Lançamento salvo com sucesso!!");
        }

    } catch (Exception e) {
        System.out.println("ERROR: " + e);
        e.printStackTrace();
    }
}
    
asked by anonymous 14.04.2017 / 22:31

1 answer

1

the itens list was not instantiated, i.e. you are trying to add an object to the list of items using the adicionaItens() method, and at no time do you instantiate the list of items.

    
15.04.2017 / 01:04