@Onetomany Bidirectional

Asked

Viewed 595 times

0

Good afternoon, you guys I am developing a financial web application and am having problems in making the relationship between Lancamento and ItemLancamento. I will post what I have done so far, giving error while adding item.

LACQUER CLASS:

@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 Itemlancamento:

@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;

MAIN CLASS()

        @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();
    }
}
  • The picture insert line, not in the code, I put here by mistake.

  • What error? adds log

  • ERROR: java.lang.Nullpointerexception java.lang.Nullpointerexception at br.edu.unilasalle.model.Release.additionItensL(Release.java:64) at br.edu.unilasalle.TesteUnitario.Lancamentodaotest.save(Lancamentodaotest.java:62) at sun.reflect..Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect.Nativemethodaccessorimpl.invoke(Unknown Source) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.Runners.model.Frameworkmethod$1.runReflectiveCall(Frameworkmethod.java:50)

  • at org.junit.Internal.runners.model.ReflectiveCallable.run(Reflectivecallable.java:12) at org.junit.Runners.model.Frameworkmethod.invokeExplosively(Frameworkmethod.java:47) at org.junit.Internal.runners.statements.InvokeMethod.evaluate(Invokemethod.java:17) at org.junit.Runners.ParentRunner.runLeaf(Parentrunner.java:325) at org.junit.Runners.Blockjunit4classrunner.runChild(Blockjunit4classrunner.java:78) at org.junit.Runners.Blockjunit4classrunner.runChild(Blockjunit4classrunner.java:57)

  • at org.junit.Runners.Parentrunner$3.run(Parentrunner.java:290) at org.junit.Runners.Parentrunner$1.Schedule(Parentrunner.java:71) at org.junit.Runners.ParentRunner.runChildren(Parentrunner.java:288) at org.junit.Runners.ParentRunner.access$000(Parentrunner.java:58) at org.junit.Runners.Parentrunner$2.evaluate(Parentrunner.java:268) at org.junit.Runners.ParentRunner.run(Parentrunner.java:363) at org.eclipse.Jdt.internal.junit4.runner.Junit4testreference.run(Junit4testreference.java:86) at org.eclipse.Jdt.internal.junit.runner.Testexecution.run(Testexecution.java:38)

  • at org.eclipse.Jdt.internal.junit.runner.Remotetestrunner.runTests(Remotetestrunner.java:459) at org.eclipse.Jdt.internal.junit.runner.Remotetestrunner.runTests(Remotetestrunner.java:678) at org.eclipse.Jdt.internal.junit.runner.Remotetestrunner.run(Remotetestrunner.java:382) at org.eclipse.Jdt.internal.junit.runner.Remotetestrunner.main(Remotetestrunner.java:192)

  • Sorry, but I had to put the mistake in some parts...

Show 2 more comments

1 answer

1


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

  • Of course.... I forgot this detail and it’s the second time I do this... beginner error...kkk ... :)

  • Franck, can you help me with another post I did? It’s about inheritance. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.