我使用的是EclipseGalileo,我想使用JPA、GlassFish2.1和MySQL5部署一个简单的应用程序。不幸的是,我找不到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());
  }

 }

}

我在servlet项目的属性中添加了JPA项目的项目引用和模块依赖项。是否有其他必须完成的配置设置?
到目前为止,我能够发布这个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

我有什么遗漏吗?

最佳答案

我认为有很多问题。
首先,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" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <jta-data-source>jdbc/sample</jta-data-source>
    <class>model.Customer</class>
</persistence-unit>
</persistence>

也就是说,提供程序字段,以及指示您正在服务器(jta数据源)中运行的必要字段。当然,jta数据源必须引用在Glassfish中配置的数据源。
接下来,我认为您的应用程序在端口4848上运行是很奇怪的,通常这是Glassfish的管理侦听器,我希望只有管理控制台在那里运行。你重新配置了你的玻璃鱼港口吗?
令我困惑的一件事是,这样的配置到底是怎么做到的:看起来Toplink认为它必须联系在本地主机上运行的Derby(端口1527是Derby的标准端口),所以可能还有其他persistence.xml存在?请检查一下。
关于教程:我经常使用Glassfish,但总是使用NetBeans。这里有几个指向Netbeans站点教程的链接,它们可能会帮助您。
http://www.netbeans.org/kb/docs/web/customer-book.html
http://www.netbeans.org/kb/61/javaee/persistence.html
安装Netbeans、遵循教程并查看生成的所有文件可能是最简单的,Netbeans会自动创建很多这样的东西,我不知道Eclipse对这些文件提供了多大程度的帮助。
以下是一个基于Eclipse的相当完整的教程:http://wiki.eclipse.org/EclipseLink/Examples/JPA/GlassFishV2_Web_Tutorial
最后一个:GF3的教程也应该让您了解GF2,至少对于这些技术(servlet和JPA)。好吧,GF3配备了eclipseelink而不是Toplink必需品,但这两个根本没有什么不同。
编辑:当我看到TLE试图连接到本地主机上的Derby时,我忘记了MySQL的部分。现在已经更正了这一点-已经删除了对如何启动Derby的引用。

10-01 22:37
查看更多