JPA无法创建(持久化)EJB [英] JPA is'nt able to create ( persist ) an EJB
问题描述
现在两天试图解决这个问题。
问题似乎来自DAO类。
Now two days trying to fix this problem. The problem seems to come from the DAO class.
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:
Error occurs when executing query. This is the 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
The error comes from the method "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;
}
知道用户表已声明。
这是bean Utilisateur。
knowing that the table Users is declared. This is the bean Utilisateur.
@Entity
@Table(name = "Users")
public class Utilisateur {...
推荐答案
消息清楚地说明问题所在:
The message tells clearly what the problem is:
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist
sql="SELECT SEQ_GEN_IDENTITY.NEXTVAL FROM DUAL")
代码是试图从名为DUAL的数据库序列中读取,但这个序列不存在。
The code is trying to read from a database sequence named "DUAL", but this one doesn't exist.
我不知道为什么会这样,你的代码中有这个:
I'm not sure why this, you have this in your code:
@GeneratedValue( strategy = GenerationType.IDENTITY )
这应告诉JPA它应该使用数据库标识列来获取它想要保留的对象的ID。
This should tell JPA that it should use the database identity column to get the ID for the object it wants to persist.
如果您没有特定的理由使用 GenerationType.IDENTITY
,则应将其更改为 Generation Type.SEQUENCE
。
If you don't have a specific reason to use GenerationType.IDENTITY
, you should change it to GenerationType.SEQUENCE
.
要做到这一点,你必须改变你的课程,如下所示:
To do that you have to change your class to look like this:
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
@Column(name = "id_emp")
private int idemp;
如果使用EclipseLink(默认),则必须创建名为seq_gen_sequence的数据库序列。如果你正在使用Hibernate,你必须创建一个名为hibernate_sequence的数据库序列。
If you are using EclipseLink (default) you have to create a database sequence named "seq_gen_sequence". If you are using Hibernate you have to create a database sequence named "hibernate_sequence".
参见:
- @Id和@GeneratedValue(strategy = GenerationType.IDENTITY)注释的用途是什么?为什么generationtype是身份?
- @ GeneratedValue(strategy =IDENTITY)vs. @GeneratedValue(strategy =SEQUENCE)
- 如何在使用JPA和Hibernate时选择id生成策略
- what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?
- @GeneratedValue(strategy="IDENTITY") vs. @GeneratedValue(strategy="SEQUENCE")
- How to choose the id generation strategy when using JPA and Hibernate
这篇关于JPA无法创建(持久化)EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!