尝试与Hibernate保持关联实体时出现Nullpointer异常

时间:2019-04-17 15:36:21

标签: hibernate java-8 h2

我正在使用H2数据库和Hibernate进行持久化,从而使用Java构建简单的应用程序。

我成功地保留了简单的实体。但是现在,我打算创建一个由“ Venda”(销售)和Produto(产品)N对N关系形成的关联实体,这将被称为Venda_produto。

但是在下面运行我的代码时,出现以下异常: “ C:\ Program Files \ Java \ jdk1.8.0_111 \ bin \ java.exe”“

-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\lib\idea_rt.jar=57490:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;C:\Users\Lucas_Pletsch\IdeaProjects\PDV2\target\classes;C:\Users\Lucas_Pletsch\.m2\repository\org\hibernate\hibernate-core\5.4.0.Final\hibernate-core-5.4.0.Final.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\Lucas_Pletsch\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;C:\Users\Lucas_Pletsch\.m2\repository\net\bytebuddy\byte-buddy\1.7.11\byte-buddy-1.7.11.jar;C:\Users\Lucas_Pletsch\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;C:\Users\Lucas_Pletsch\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\Lucas_Pletsch\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.1.0.Final\hibernate-commons-annotations-5.1.0.Final.jar;C:\Users\Lucas_Pletsch\.m2\repository\javax\xml\bind\jaxb-api\2.3.0\jaxb-api-2.3.0.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\glassfish\jaxb\jaxb-runtime\2.3.1\jaxb-runtime-2.3.1.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\glassfish\jaxb\txw2\2.3.1\txw2-2.3.1.jar;C:\Users\Lucas_Pletsch\.m2\repository\com\sun\istack\istack-commons-runtime\3.0.7\istack-commons-runtime-3.0.7.jar;C:\Users\Lucas_Pletsch\.m2\repository\org\jvnet\staxex\stax-ex\1.8\stax-ex-1.8.jar;C:\Users\Lucas_Pletsch\.m2\repository\com\sun\xml\fastinfoset\FastInfoset\1.2.15\FastInfoset-1.2.15.jar;C:\Users\Lucas_Pletsch\.m2\repository\com\h2database\h2\1.4.197\h2-1.4.197.jar" Main.Main
Falha ao adicionar objeto ao banco de dados. java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
    at Persistencia.DAOGeral.registrarNoBd(DAOGeral.java:26)
    at Main.Main.main(Main.java:40)

Process finished with exit code 1

亲戚,关系中的主导阶层:Venda

package ProcessoDeVenda;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Entity
public class Venda {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long cod;


    @ManyToMany
    @JoinTable(name="Produtos_da_Venda", joinColumns =
            {@JoinColumn(name="Venda_Id")}, inverseJoinColumns =
            {@JoinColumn(name="Produto_Id")})
    private Set<Produto> produtos;


    private double valor;
    LocalDateTime dataHoraVenda;


    public Venda(Set<Produto> produtos, double valor){
        this.produtos = produtos;
        this.dataHoraVenda = LocalDateTime.now();
        this.valor = valor;
    }

Produto类:

package ProcessoDeVenda;

import javax.persistence.*;
import java.sql.Date;

@Entity
public class Produto {


    private String nome;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long cod;

    private double valor;
    private Date dataValidade;

    protected Produto() {
    }

    public Produto(String nome, double valor, Date dataValidade) {
        this.nome = nome;
        this.valor = valor;
        this.dataValidade = dataValidade;
    }
}

这是我的Main类,在其中创建对象:

    public class Main {

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            Date validadeProd1 = new Date(30, 06, 2019);

            Produto prod1 = new Produto("Nescau", 9.90, validadeProd1);
            Produto prod2 = new Produto("Maçã", 5.90, validadeProd1);
            Produto prod3 = new Produto("Banana", 3.40, validadeProd1);

            HashSet<Produto> produtos = new HashSet();
            produtos.add(prod1);
            produtos.add(prod2);
            produtos.add(prod3);

            Venda vendaTeste = new Venda(produtos, 19.20);

            VendaDAO vendaDAO = new VendaDAO();
            vendaDAO.registrarNoBd(vendaTeste);

      }
}

我的DAO类非常坚持: (我不会在会议期间将工厂类“ BancoDeDados”放到这里,因为它运行良好)

package Persistencia;

import org.hibernate.Session;
import org.hibernate.Transaction;

import javax.persistence.Query;

public abstract class DAOGeral {


    BancoDeDados banco = BancoDeDados.getBanco();


    public void registrarNoBd(Object objeto) {


        Transaction tx = null;
        Session sessao = null;
        try {
            sessao = banco.getUmaSessao();
            tx = sessao.beginTransaction();
            sessao.persist(objeto);
            tx.commit();
        }catch (Exception e){
            System.out.println("Falha ao adicionar objeto ao banco de dados. " + e);
            tx.rollback();
        }

        sessao.close();

    }
}

下面是我的H2 Databse控制台,其中已经创建了两个表。请注意,现在我的代码中“ Venda”类中有一个新的属性:“ dataHoraVenda”。但这不是问题,一旦hibernate.properties文件具有“ hibernate.hbm2ddl.auto =更新”。

enter image description here

我已经搜索了代码,看起来一切正常,我不知道为什么会发生问题...

0 个答案:

没有答案