JPA无法创建(持久化)EJB

时间:2017-04-28 13:40:11

标签: jpa java-ee glassfish ejb

现在两天试图解决这个问题。 问题似乎来自DAO课程。

Caused by: projet.helpdesk.dao.DAOException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

Error Code: 2289
Call: SELECT SEQ_GEN_IDENTITY.NEXTVAL FROM DUAL
Query: ValueReadQuery(sql="SELECT SEQ_GEN_IDENTITY.NEXTVAL FROM DUAL")
    at projet.helpdesk.dao.UserDao.creer(UserDao.java:25)

这是实体:

    package projet.helpdesk.beans;

import java.sql.Timestamp;
import javax.persistence.*;
@Entity
@Table(name = "Users")
public class Utilisateur {
    @Column(name = "nom")
    private String nom;
    @Column(name = "prenom")
    private String prenom;
    @Column(name = "email")
    private String email;
    @Column(name = "departement")
    private String dept;
    @Column(name = "poste")
    private String poste;
    @Column(name = "agence")
    private String agence;
    @Column(name = "mdp")
    private String mdp;
    @Column(name = "type")
    private String type;
    @Column(name = "date_inscr")
    private Timestamp date_inscr;

    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    @Column(name = "id_emp")
    private int idemp;
    public String getNom() {
        return nom;
    }
    public void setNom(String nom) {
        this.nom = nom;
    }
    public String getPrenom() {
        return prenom;
    }
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    public String getPoste() {
        return poste;
    }
    public void setPoste(String poste) {
        this.poste = poste;
    }
    public String getAgence() {
        return agence;
    }
    public void setAgence(String agence) {
        this.agence = agence;
    }
    public int getIdemp() {
        return idemp;
    }
    public void setIdemp(int id) {
        this.idemp = id;
    }
    public String getMdp() {
        return mdp;
    }
    public void setMdp(String mdp) {
        this.mdp = mdp;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Timestamp getDate_inscr() {
        return date_inscr;
    }
    public void setDate_inscr(Timestamp date_inscr) {
        this.date_inscr = date_inscr;
    }

}

编辑:执行查询时出错。 这是Stacktrace:

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT u FROM Users u WHERE u.email=:email]. 
[14, 19] The abstract schema type 'Users' is unknown.
[28, 35] The state field path 'u.email' cannot be resolved to a valid type.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1616)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:456)
    at projet.helpdesk.dao.UserDao.trouver(UserDao.java:32)

错误来自方法" trouver"

@Stateless
public class UserDao {
    private static final String JPQL_SELECT_PAR_EMAIL = "SELECT u FROM Users u WHERE u.email=:email";
    private static final String PARAM_EMAIL           = "email";

这是方法"麻烦"

 public Utilisateur trouver( String email ) throws DAOException {
        Utilisateur utilisateur = null;
        Query requete = em.createQuery( JPQL_SELECT_PAR_EMAIL );
        requete.setParameter( PARAM_EMAIL, email );
        try {
            utilisateur = (Utilisateur) requete.getSingleResult();
        } catch ( NoResultException e ) {
            return null;
        } catch ( Exception e ) {
            throw new DAOException( e );
        }
        return utilisateur;
    }

知道表User已声明。 这是bean Utilisateur。

@Entity
@Table(name = "Users")
public class Utilisateur {...

1 个答案:

答案 0 :(得分:2)

该消息清楚地说明问题所在:

Internal Exception: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

sql="SELECT SEQ_GEN_IDENTITY.NEXTVAL FROM DUAL")

代码试图从名为" SEQ_GEN_IDENTITY"的数据库序列中读取,但这个数据库不存在。

我不知道为什么会这样,你的代码中有这个:

@GeneratedValue( strategy = GenerationType.IDENTITY )

这应该告诉JPA它应该使用数据库标识列来获取它想要保留的对象的ID。

如果您没有特定的理由使用GenerationType.IDENTITY,则应将其更改为GenerationType.SEQUENCE

要做到这一点,你必须改变你的课程:

@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
@Column(name = "id_emp")
private int idemp;

如果您正在使用EclipseLink(默认),则必须创建名为" seq_gen_sequence"的数据库序列。如果您正在使用Hibernate,则必须创建一个名为" hibernate_sequence"的数据库序列。

另见: