Mysql 如何在Eclipse中配置Servlet以使用JPA项目?
我正在使用EclipseGalileo,我想部署一个简单的应用程序,使用JPA、GlassFish2.1和MySQL5。不幸的是,我找不到GlassFish2.1的任何教程(仅针对3.0,但我无法使用它) 我创建了一个JPA项目,添加了一个MySQL5连接,并从数据库生成了一个实体 生成JPA类是:Mysql 如何在Eclipse中配置Servlet以使用JPA项目?,mysql,eclipse,servlets,jpa,glassfish,Mysql,Eclipse,Servlets,Jpa,Glassfish,我正在使用EclipseGalileo,我想部署一个简单的应用程序,使用JPA、GlassFish2.1和MySQL5。不幸的是,我找不到GlassFish2.1的任何教程(仅针对3.0,但我无法使用它) 我创建了一个JPA项目,添加了一个MySQL5连接,并从数据库生成了一个实体 生成JPA类是: package model; import java.io.Serializable; import javax.persistence.*; @Entity @Table(name="cust
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>
我在servlet项目的属性中添加了JPA项目的项目引用和模块依赖项。是否必须进行任何其他配置设置?
到目前为止,我能够发布Servlet,但不幸的是,我无法运行它,我听到“你好,世界!”消息,但如果我要访问,则会出现以下异常:
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看起来有点奇怪,我本以为会是这样的:
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());
}
}
}
<?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>
oracle.toplink.essentials.PersistenceProvider
jdbc/示例
模型。客户
也就是说,提供程序字段,以及指示您正在服务器(jta数据源)中运行的必要字段。当然,jta数据源必须引用您在Glassfish中配置的数据源
接下来,我认为您的应用程序在端口4848上运行非常奇怪,通常这是Glassfish的管理侦听器,我希望只有管理控制台在那里运行。你重新配置了玻璃鱼的端口了吗
让我困惑的一件事是,您是如何通过这样的配置走到这一步的:看起来Toplink认为它必须联系本地主机上运行的Derby(端口1527是Derby的标准端口),所以可能还有其他一些persistence.xml浮动?请检查一下
关于教程:我经常使用Glassfish,但总是使用NetBeans。这里有几个指向Netbeans站点教程的链接,它们可能会对您有所帮助