0

I'm using Eclipse Juno, Glassfish 3.1.2, and MySQL 5.1.

I'm building a simple EJB & JSF application. I created the following eclipse projects:

  • appEAR <-- the EAR file
  • appEJB <-- contains UserService.java EJB
  • appJPA <-- contains UserDAO.java EJB, and User.java object
  • appWeb <-- contains index.jsp

It's just a skeleton right now, but I can deploy the app and see the index.jsp

Next, I tried to add the following to the UserDAO ...

@PersistenceContext
EntityManager em;

But then when the app tries to republish, it gives me the error:

'Publishing to GlassFish 3.1.2 at localhost...' has encountered a problem.  cannot Deploy appEar

There are no other details.

When I remove the two lines of @PersistenceContent code, the app deploys again.

Also, the persistence.xml file n the appJPA project is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="appJPA">
            <class>app.model.User</class>
    </persistence-unit>
</persistence>

Please help ... what am I missing? I'm rather stuck.

3 Answers3

1

Your persistence.xml is incomplete , you need to provide Connection properties to specify the provider ,which DB to connect etc

Heres an example using hibernate as the JPA provided

<persistence-unit name="educationPU"
    transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.coe.jpa.StudentProfile</class>
    <properties>
        <property name="hibernate.connection.driver_class"
            value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url"
            value="jdbc:mysql://localhost:3306/COE" />
        <property name="hibernate.connection.username" value="root" />
        <property name="show_sql" value="true" />
        <property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>

and heres a more Generic one

I am very new to glassfish, JPA and so on and I have really problems with setting that up. What I am planning to do is a simple RESTful service with a persistent backend. I am using glassfish3 as application server and already deployed a simple REST service with the jersey-library. Now I want to provide access to a database via JPA. Glassfish is shipped with JavaDB/derby and EclipseLink, is that right? So, I want to use that :-)

I created a persistence.xml in META-INF:

<?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="myPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDataSource" /> <!-- org.apache.derby.jdbc.EmbeddedDriver -->
      <property name="javax.persistence.jdbc.url"      value="jdbc:derby://localhost:1527/sample;create=true" />
      <property name="javax.persistence.jdbc.user"     value="APP" />
      <property name="javax.persistence.jdbc.password" value="APP" />
      <property name="eclipselink.ddl-generation"      value="create-tables" />
    </properties>
  </persistence-unit>
</persistence>
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
0

I don't use glassfish. But I think the reason is that you didn't specify any datasource in the persistence.xml. you should do this in it, which you can use jndi or other way. and second, you should define the entityManagerFactory bean in spring context xml file.

OQJF
  • 1,350
  • 9
  • 12
0

Did you add the datasource in glasfish ? You will need to add the mysql jdbc drivers too. In Java EE, it's the persistence container (inside the server) which will create and manage the datasource for you.

See http://www.albeesonline.com/blog/2008/08/06/creating-and-configuring-a-mysql-datasource-in-glassfish-application-server/

Gab
  • 7,869
  • 4
  • 37
  • 68