【发布时间】:2010-12-09 22:54:06
【问题描述】:
我正在使用 Eclipse Galileo,我想使用 JPA、GlassFish 2.1 和 MySQL 5 部署一个简单的应用程序。不幸的是,我找不到 GlassFish 2.1 的任何教程(仅适用于 3.0,但我无法使用它)。
我创建了一个 JPA 项目,添加了一个 MySQL5 连接并从数据库生成了一个实体。
生成的JPA类是:
package model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="customer")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="customer_id")
private int customerId;
private String email;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
public Customer() {
}
public int getCustomerId() {
return this.customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
而persistence.xml文件是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="JPAProject2">
<class>model.Customer</class>
</persistence-unit>
</persistence>
我创建了一个动态 Web 项目,并添加了一个新的 Servlet 类,如下所示:
package servlet;
import java.io.IOException;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import model.Customer;
public class JpaDemoServlet2 extends HttpServlet
{
private static final long serialVersionUID = 1L;
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
@Resource
private UserTransaction userTransaction;
public JpaDemoServlet2()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
EntityManager entityManager =
entityManagerFactory.createEntityManager();
Customer customer = new Customer();
customer.setCustomerId(3);
customer.setFirstName("Smith");
customer.setLastName("John");
customer.setEmail("john.smith@email.com");
try
{
userTransaction.begin();
entityManager.persist(customer);
userTransaction.commit();
}
catch(Exception ex)
{
response.sendError(1, ex.getMessage());
}
}
}
我为 JPA 项目添加了 servlet 项目的属性 Project References 和 Module Dependencies。是否有任何其他必须完成的配置设置? 到目前为止,我能够发布 Servlet,但不幸的是,我无法运行它。 http://localhost:4848/ServletProject2,我得到了“你好,世界!”消息,但如果我想访问 http://localhost:4848/ServletProject2/JpaDemoServlet2 我得到这个异常:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Error Code: 0
我有什么遗漏吗?
【问题讨论】:
标签: mysql eclipse servlets jpa glassfish