Я новичок в JPA, и у меня есть база данных UserRecords с USER_ID в качестве моего первичного ключа, и я установил для него автоинкремент.Однако всякий раз, когда я запускаю программу, она выдает мне ошибку:
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'USERRECORDS' does not exist.
Error Code: 30000
Call: INSERT INTO UserRecords (Email, FirstName, LastName, Password) VALUES (?, ?, ?, ?)
bind => [4 parameters bound]
Query: InsertObjectQuery(com.Exoticatravels.UserRecords[ userId=null ])
Код для моего класса сущности:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.Exoticatravels;
import java.io.Serializable;
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.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author sufiyaan
*/
@Entity
@Table(name = "UserRecords")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "UserRecords.findAll", query = "SELECT u FROM UserRecords u")
, @NamedQuery(name = "UserRecords.findByUserId", query = "SELECT u FROM UserRecords u WHERE u.userId = :userId")
, @NamedQuery(name = "UserRecords.findByFirstName", query = "SELECT u FROM UserRecords u WHERE u.firstName = :firstName")
, @NamedQuery(name = "UserRecords.findByLastName", query = "SELECT u FROM UserRecords u WHERE u.lastName = :lastName")
, @NamedQuery(name = "UserRecords.findByEmail", query = "SELECT u FROM UserRecords u WHERE u.email = :email")
, @NamedQuery(name = "UserRecords.findByPassword", query = "SELECT u FROM UserRecords u WHERE u.password = :password")})
public class UserRecords implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "USER_ID")
private Integer userId;
@Size(max = 50)
@Column(name = "FirstName")
private String firstName;
@Size(max = 50)
@Column(name = "LastName")
private String lastName;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 100)
@Column(name = "Email")
private String email;
@Size(max = 50)
@Column(name = "Password")
private String password;
public UserRecords() {
}
public UserRecords(Integer userId) {
this.userId = userId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
int hash = 0;
hash += (userId != null ? userId.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 UserRecords)) {
return false;
}
UserRecords other = (UserRecords) object;
if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.Exoticatravels.UserRecords[ userId=" + userId + " ]";
}
}
Код для моего постоянного 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="Exotica_TravelsPU" transaction-type="JTA">
<jta-data-source>java:app/ExoticaTravels</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Код для моего сервлета:
public class Register extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
String pass = request.getParameter("pass");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Exotica_TravelsPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
UserRecords users = new UserRecords();
users.setFirstName(firstName);
users.setLastName(lastName);
users.setEmail(email);
users.setPassword(pass);
em.persist(users);
em.getTransaction().commit();
em.close();
emf.close();
HttpSession session = request.getSession();
session.setAttribute("firstName", firstName);
RequestDispatcher rd = request.getRequestDispatcher("/welcome.jsp");
rd.forward(request, response);
}
}
Программа работает правильно, когда у меня нет USER_ID в качестве автоматического приращения.
Руководство будет очень полезно.
Заранее спасибо!