2009-10-25 30 views
2

我正在使用Eclipse Galileo,我想部署一個簡單的應用程序,使用JPA,GlassFish 2.1和MySQL 5.不幸的是,我找不到GlassFish 2.1的任何教程(僅適用於3.0,但我無法使用它)。如何在Eclipse中配置Servlet以使用JPA項目?

我創建了一個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("[email protected]"); 

    try 
    { 
    userTransaction.begin(); 
    entityManager.persist(customer); 
    userTransaction.commit(); 
    } 
    catch(Exception ex) 
    { 
    response.sendError(1, ex.getMessage()); 
    } 

} 

} 

我在servlet項目的屬性中添加了Project References和Module Dependencie s爲JPA項目。是否有任何其他配置設置必須完成? 到目前爲止,我能夠發佈Servlet,但不幸的是,我無法運行它。 http://localhost:4848/ServletProject2,我收到'Hello,World!'消息,但如果我想訪問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 

有什麼我失蹤?

回答

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認爲它必須聯繫在本地主機上運行的Derby(端口1527對於Derby是標準的),所以也許還有一些其他的持久性。 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嘗試連接到本地主機上的Derby時,我忘記了關於MySQL的部分。現在已經得到糾正 - 關於如何啓動德比的提法已被刪除。

相關問題