我正在使用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());
}
}
}
我在servlet项目的属性Project References和Module Dependencies中添加了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
我有什么遗失的吗?
答案 0 :(得分:2)
我认为有很多问题。
首先,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-data-source)。当然,jta-data-source必须引用您在Glassfish中配置的数据源。
接下来,我认为您的应用程序在端口4848上运行是很奇怪的,通常是Glassfish的管理侦听器,我希望只有管理控制台才能在那里运行。您是否重新配置了Glassfish的端口?
令我困惑的一件事是你如何通过这样的配置实现这一目标:看起来Toplink认为它必须联系在localhost上运行的Derby(端口1527是Derby的标准)所以也许还有一些其他的persistence.xml漂浮?请检查一下。
关于教程:我经常使用Glassfish,但总是使用NetBeans。以下是Netbeans网站教程的几个链接,它们可能会对您有所帮助。
安装Netbeans可能最简单,按照教程查看生成的所有文件,Netbeans自动创建了很多这样的东西,我不知道Eclipse给你带来了多大程度的帮助这些文件。
这是一个基于Eclipse的相当完整的教程:http://wiki.eclipse.org/EclipseLink/Examples/JPA/GlassFishV2_Web_Tutorial
最后一个:GF3的教程也应该让你继续使用GF2,至少对于这些技术(servlet和JPA)。好的,GF3附带了Eclipselink而不是Toplink Essentials,但这两者完全不同。
编辑:当我看到TLE尝试连接到localhost上的Derby时,我忘了关于MySQL的部分。现在已经更正了 - 已经删除了有关如何启动Derby的参考。