为什么我会遇到异常:" ...不是已知的实体类型"?

时间:2016-07-31 04:28:06

标签: java jpa eclipselink

我需要有关代码段的帮助。我正在使用JPA提供程序Ec​​lipseLink。我有很多课程,所有工作都有,但这是唯一一个让我误认为它不是实体的人。

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JavaApplication3PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>Models.Cliente</class>
<class>Models.Compra</class>
<class>Models.Factura</class>
<class>Models.Producto</class>
<class>Models.Proveedor</class>
<class>Models.TipoVenta</class>
<class>Models.Venta</class>
<class>Models.Inventario</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/root/NetBeansProjects/JavaApplication3/DataBase.db"/>
  <property name="javax.persistence.jdbc.user" value=""/>
  <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/>
  <property name="javax.persistence.jdbc.password" value=""/>
  <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>

和我的班级&#34; Inventario&#34;

package Models;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Michel Betancourt
 * @version 1.0
 */
@Entity
@Table(name = "Inventario", catalog = "", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Inventario.findAll", query = "SELECT i FROM Producto i"),
    @NamedQuery(name = "Inventario.findByProducto", query = "SELECT i FROM Inventario i WHERE i.idInventario = :idInventario")})
public class Inventario implements Serializable, Conexion {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "sqlite_inventario")
    @TableGenerator(name = "sqlite_inventario", table = "sqlite_sequence",
        pkColumnName = "name", valueColumnName = "seq",
        pkColumnValue = "sqlite_inventario",
        initialValue = 0, allocationSize = 1)
    @Column(name = "idInventario")
    private Integer idInventario;

    @Basic(optional = false)
    @Column(name = "stockInventario")
    private Integer stock;


    @OneToMany(mappedBy = "inventario")
    private List<Producto> productos;

    //----------------------------------------------------------------------------------------------------------------//
    //  Constructores
    //----------------------------------------------------------------------------------------------------------------//
    /**
     * Constructor por defecto.
     */
    public Inventario() {
    }

    /**
     * Crea un inventario.
     * @param stock Cantidad de productos
     */
    public Inventario(Integer stock) {
        this.stock = stock;
        save();
    }


    //----------------------------------------------------------------------------------------------------------------//
    //  Metodos estaticos
    //----------------------------------------------------------------------------------------------------------------//
    /**
     * Busca todos los inventarios.
     * @return List con todos los inventarios.
     */
    public static List<Inventario> findAll(){
        return EM.createNamedQuery("Inventario.findAll").getResultList();
    }

    /**
     * Todos los productos existentes.
     * @return cantidad de productos en almacenes.
     */
    public static int countStock(){
        List<Inventario> inventarios = findAll();
        int total = 0;
        return inventarios.stream().map((next) -> next.stock).reduce(total, Integer::sum);
    }

    //----------------------------------------------------------------------------------------------------------------//
    //  Metodos
    //----------------------------------------------------------------------------------------------------------------//
    /**
     * Agrega nuevos productos al inventario.
     * @param cantidad Cuanto se va a agregar
     * @return True: Si se agrega con éxito. False: Si no hay capacidad.
     */
    public boolean addStock(int cantidad){
        int total = countStock();
        if(total + cantidad <= Config.getCapacidadStock()){
            stock += cantidad;
            save();
            return true;
        }
        return false;
    }

    /**
     * Quita productos del inventario.
     * @param cantidad Cuanto se va a quitar.
     * @return True: Si se quitan con éxito. False: Si se trata de eliminar mas de el stock actual.
     */
    public boolean remStock(int cantidad){
        if(stock - cantidad >= 0){
            stock -= cantidad;
            save();
            return true;
        }
        return false;
    }

    //----------------------------------------------------------------------------------------------------------------//
    //  Metodos de guardar y borrar
    //----------------------------------------------------------------------------------------------------------------//
    /**
     * Elimina una compra de la base de datos.
     */
    public void delete(){
        EM.getTransaction().begin();
        EM.remove(this);
        EM.getTransaction().commit();
    }

    /**
     * Guarda el objeto en la base de datos.
     */
    private void save(){
        EM.getTransaction().begin();
        EM.persist(this);
        EM.flush();
        EM.getTransaction().commit();
    }

    //----------------------------------------------------------------------------------------------------------------//
    //  Getters and Setters
    //----------------------------------------------------------------------------------------------------------------//
    public Integer getIdInventario() {
        return idInventario;
    }

    public void setIdInventario(Integer idInventario) {
        this.idInventario = idInventario;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    @XmlTransient
    public List<Producto> getProductos() {
        return productos;
    }

    public void setProductos(List<Producto> productos) {
        this.productos = productos;
    }

    //----------------------------------------------------------------------------------------------------------------//
    //  Sobre escritura de metodos
    //----------------------------------------------------------------------------------------------------------------//
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idInventario != null ? idInventario.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the idInventario fields are not set
        if (!(object instanceof Inventario)) {
            return false;
        }
        Inventario other = (Inventario) object;
        return !((this.idInventario == null && other.idInventario != null) || (this.idInventario != null && !this.idInventario.equals(other.idInventario)));
    }

    @Override
    public String toString() {
        return "Models.Inventario[ id=" + idInventario + " ]";
    }

}

我搜索并搜索但找不到错误。相应的StackTrace如下:

Exception in thread "main" java.lang.IllegalArgumentException: Object: Models.Inventario[ id=null ] is not a known entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
    at Models.Inventario.save(Inventario.java:155)
    at Models.Inventario.<init>(Inventario.java:81)
    at javaapplication3.JavaApplication3.main(JavaApplication3.java:36)
Java Result: 1

拜托,我的错误是什么?

0 个答案:

没有答案