JPA 无法创建(持久化)EJB
JPA is'nt able to create ( persist ) an EJB
现在两天试图解决这个问题。
问题似乎来自 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;
}
}
编辑: 执行查询时发生错误。
这是堆栈跟踪:
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";
这是方法"trouver"
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;
}
知道 table 用户已声明。
这是bean Utilisateur。
@Entity
@Table(name = "Users")
public class Utilisateur {...
消息清楚地说明了问题所在:
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
。
为此,您必须将 class 更改为如下所示:
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
@Column(name = "id_emp")
private int idemp;
如果您使用 EclipseLink(默认),您必须创建一个名为 "seq_gen_sequence" 的数据库序列。如果您使用的是 Hibernate,则必须创建一个名为 "hibernate_sequence".
的数据库序列
另请参阅:
- what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?
- @GeneratedValue(strategy=“IDENTITY”) 与@GeneratedValue(strategy=“SEQUENCE”)
- How to choose the id generation strategy when using JPA and Hibernate
现在两天试图解决这个问题。 问题似乎来自 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;
}
}
编辑: 执行查询时发生错误。 这是堆栈跟踪:
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";
这是方法"trouver"
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;
}
知道 table 用户已声明。 这是bean Utilisateur。
@Entity
@Table(name = "Users")
public class Utilisateur {...
消息清楚地说明了问题所在:
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
。
为此,您必须将 class 更改为如下所示:
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
@Column(name = "id_emp")
private int idemp;
如果您使用 EclipseLink(默认),您必须创建一个名为 "seq_gen_sequence" 的数据库序列。如果您使用的是 Hibernate,则必须创建一个名为 "hibernate_sequence".
的数据库序列另请参阅:
- what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?
- @GeneratedValue(strategy=“IDENTITY”) 与@GeneratedValue(strategy=“SEQUENCE”)
- How to choose the id generation strategy when using JPA and Hibernate