Неизвестный класс сущности бина - ошибка с Glassfish и NetBeans - PullRequest
0 голосов
/ 04 января 2012

Я использую GlassFish 3.1.1. и NetBeans 6.9.1. В NetBeans я создал веб-приложение и создал класс сущностей. Единственное, что я добавил, это аннотация @GeneratedValue. Вот код:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities.worker;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author pzielins
 */
@Entity
@Table(name = "cdrconfig", catalog = "cdrworker_db", schema = "public")
@NamedQueries({
    @NamedQuery(name = "CdrConfig.findAll", query = "SELECT c FROM CdrConfig c"),
    @NamedQuery(name = "CdrConfig.findById", query = "SELECT c FROM CdrConfig c WHERE c.id = :id"),
    @NamedQuery(name = "CdrConfig.findByCdrpath", query = "SELECT c FROM CdrConfig c WHERE c.cdrpath = :cdrpath"),
    @NamedQuery(name = "CdrConfig.findByWorkerinterval", query = "SELECT c FROM CdrConfig c WHERE c.workerinterval = :workerinterval"),
    @NamedQuery(name = "CdrConfig.findByMakebackup", query = "SELECT c FROM CdrConfig c WHERE c.makebackup = :makebackup"),
    @NamedQuery(name = "CdrConfig.findByBackuppath", query = "SELECT c FROM CdrConfig c WHERE c.backuppath = :backuppath"),
    @NamedQuery(name = "CdrConfig.findByLastsyncdate", query = "SELECT c FROM CdrConfig c WHERE c.lastsyncdate = :lastsyncdate"),
    @NamedQuery(name = "CdrConfig.findByWaslastsyncmanual", query = "SELECT c FROM CdrConfig c WHERE c.waslastsyncmanual = :waslastsyncmanual")})
public class CdrConfig implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Column(name = "cdrpath", length = 10485760)
    private String cdrpath;
    @Column(name = "workerinterval")
    private Integer workerinterval;
    @Column(name = "makebackup")
    private Boolean makebackup;
    @Column(name = "backuppath", length = 10485760)
    private String backuppath;
    @Column(name = "lastsyncdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastsyncdate;
    @Column(name = "waslastsyncmanual")
    private Boolean waslastsyncmanual;

    public CdrConfig() {
    }

    public CdrConfig(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCdrpath() {
        return cdrpath;
    }

    public void setCdrpath(String cdrpath) {
        this.cdrpath = cdrpath;
    }

    public Integer getWorkerinterval() {
        return workerinterval;
    }

    public void setWorkerinterval(Integer workerinterval) {
        this.workerinterval = workerinterval;
    }

    public Boolean getMakebackup() {
        return makebackup;
    }

    public void setMakebackup(Boolean makebackup) {
        this.makebackup = makebackup;
    }

    public String getBackuppath() {
        return backuppath;
    }

    public void setBackuppath(String backuppath) {
        this.backuppath = backuppath;
    }

    public Date getLastsyncdate() {
        return lastsyncdate;
    }

    public void setLastsyncdate(Date lastsyncdate) {
        this.lastsyncdate = lastsyncdate;
    }

    public Boolean getWaslastsyncmanual() {
        return waslastsyncmanual;
    }

    public void setWaslastsyncmanual(Boolean waslastsyncmanual) {
        this.waslastsyncmanual = waslastsyncmanual;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

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

    @Override
    public String toString() {
        return "entities.worker.CdrConfig[id=" + id + "]";
    }

}

Затем я сгенерировал класс JPAController для класса Entity. Вот код:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpa.worker;

import entities.worker.CdrConfig;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import jpa.exceptions.NonexistentEntityException;
import jpa.exceptions.PreexistingEntityException;

/**
 *
 * @author pzielins
 */
public class CdrConfigJpaController {

    public CdrConfigJpaController() {
        emf = Persistence.createEntityManagerFactory("CDRFileWorkerPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CdrConfig cdrConfig) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(cdrConfig);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCdrConfig(cdrConfig.getId()) != null) {
                throw new PreexistingEntityException("CdrConfig " + cdrConfig + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CdrConfig cdrConfig) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            cdrConfig = em.merge(cdrConfig);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cdrConfig.getId();
                if (findCdrConfig(id) == null) {
                    throw new NonexistentEntityException("The cdrConfig with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CdrConfig cdrConfig;
            try {
                cdrConfig = em.getReference(CdrConfig.class, id);
                cdrConfig.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cdrConfig with id " + id + " no longer exists.", enfe);
            }
            em.remove(cdrConfig);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CdrConfig> findCdrConfigEntities() {
        return findCdrConfigEntities(true, -1, -1);
    }

    public List<CdrConfig> findCdrConfigEntities(int maxResults, int firstResult) {
        return findCdrConfigEntities(false, maxResults, firstResult);
    }

    private List<CdrConfig> findCdrConfigEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CdrConfig.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CdrConfig findCdrConfig(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CdrConfig.class, id);
        } finally {
            em.close();
        }
    }

    public int getCdrConfigCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CdrConfig> rt = cq.from(CdrConfig.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}

Поскольку мое приложение должно работать с двумя базами данных, я отредактировал сгенерированный модулем постоянства NetBeans, и теперь у меня есть что-то вроде этого:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="CDRFileWorkerPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/cdrworkerdb</jta-data-source>
    <class>entities.config.CdrConfig</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
  <persistence-unit name="CDRDataStorePU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/cdrdb</jta-data-source>
    <class>entities.cdr.CallRecord</class>
    <class>entities.cdr.RecordColumn</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

До сих пор все было хорошо, но когда я пытаюсь использовать этот метод в моем управляемом компоненте JSF:

public void test() {
        try {
            CdrConfig cfg = new CdrConfig();
            cfg.setBackuppath("foo");
            CdrConfigJpaController cjc = new CdrConfigJpaController();
            cjc.create(cfg);
            Record rec = new Record();
            RecordJpaController rjc = new RecordJpaController();
            rjc.create(rec);
            RecordColumn col = new RecordColumn();
            col.setColumnname("foo1");
            col.setRecord(rec);
            RecordColumnJpaController rcjc = new RecordColumnJpaController();
            rcjc.create(col);
        } catch (Exception ex) {
            Logger.getLogger(FileWorkerBackup.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Я получаю следующую ошибку:

java.lang.IllegalArgumentException: Unknown entity bean class: class entities.worker.CdrConfig, please verify that this class has been marked with the @Entity annotation.
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:576)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:460)
        at jpa.worker.CdrConfigJpaController.findCdrConfig(CdrConfigJpaController.java:125)
        at jpa.worker.CdrConfigJpaController.create(CdrConfigJpaController.java:43)
        at web.FileWorkerBackup.test(FileWorkerBackup.java:52)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at...

От Google я знаю, что иногда полезно пометить «Включить все классы сущностей в модуль» в редакторе модуля постоянства, но тогда это дает мне ошибку о некоторых перекрестных ссылках между таблицами (я уверен, я не пытаюсь это сделать). Я также обнаружил в Google, что перезапуск Glassfish помогает (из-за закрытых сессий персистентности), но это также не помогает. Пожалуйста, укажите мне правильное направление.

1 Ответ

0 голосов
/ 04 января 2012

Может быть, эта строка в XML

<class>entities.config.CdrConfig</class>

должна быть

<class>entities.worker.CdrConfig</class>
...