2

I am developing a sample Java EE applicaiton with Intellij Idea 13. Simply, my project is a Maven project and has JPA end EJB modules. But when deploying and running application on Glassfish server I get errors.

I created a Maven project named SimpleEE and added JPA, EJB modules.

Here is my project structure:

|-SampleEE
|    |+.idea
|    |-src
|    |    |-main
|    |    |    |-java
|    |    |    |    |-beans
|    |    |    |    |   |-MyBean.java
|    |    |    |    |-entities
|    |    |    |        |-Staff.java
|    |    |    |-resources
|    |    |         |-META-INF
|    |    |             |-ejb-jar.xml
|    |    |             |-persistence.xml
|    |    |+test
|    |-pom.xml
|    |-SampleEE.iml
|    |-SampleEE.jpr
|+ExternalLibraries                        

MyBean.java:

package beans;
import entities.Staff;

import javax.annotation.PostConstruct;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

@Startup
@LocalBean
@Singleton
public class MyBean {
    public MyBean() {
    }

    @PostConstruct
    public void myMain() {
        EntityManager em = Persistence.createEntityManagerFactory("mysqlPU").createEntityManager();
        em.getTransaction().begin();
        Staff staff = new Staff();
        staff.setAge(44);
        staff.setGender("M");
        staff.setSalary(123);
        staff.setName("Staff Name");
        em.persist(staff);
        em.getTransaction().commit();
    }
}

Staff.java

package entities;

import javax.persistence.*;

@Entity
public class Staff {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "STAFF_ID")
    private int id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "AGE")
    private Integer age;

    @Column(name = "GENDER")
    private String gender;

    @Column(name = "SALARY")
    private Integer salary;

    @Lob
    @Column(name = "IMAGE", nullable = true)
    private byte[] image;

    // Getters and setters
    // ...       

    @Override
    public int hashCode() {
        // implementation ignored here for simplicity     
    }

    @Override
    public boolean equals(Object o) {
        // implementation ignored here for simplicity   
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="mysqlPU">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>entities.Staff</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dbName"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="mysqlUsername"/>
            <property name="javax.persistence.jdbc.password" value="mysqlPassword"/>
        </properties>
    </persistence-unit>
</persistence>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SampleEE</groupId>
    <artifactId>SampleEE</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
    </dependencies>
</project>

I created an EJB Application:Exploded artifact from project structure. In Run/Debug configurations window I built a Glassfish configuration. But when running this configuration, Idea complains that it can not find classes in mysql jdbc driver and shows errors emerging from this error. Server is started but application is not deployed. Error message snippet from Idea output log:

....
[2013-12-13 04:07:31,362] Artifact SampleEE:ejb exploded: Artifact is being deployed, please wait...
[2013-12-13 04:07:33,011] Artifact SampleEE:ejb exploded: Error during artifact deployment. See server log for details.
[2013-12-13 04:07:33,018] Artifact SampleEE:ejb exploded: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : javax.ejb.CreateException: Initialization failed for Singleton MyBean. Please see server.log for more details.

Last error in Glassfish log

....
Caused by: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error.  Class [com.mysql.jdbc.Driver] not found.
....

I guess jar files of libraries are not deployed. But my maven configuration is valid. M2_HOME is set correctly and IDEA resolves maven path. The jar files are also available in local repository.

I created a same structured project in Netbeans and it runs succesfully. I don't know what I am doing wrong or what configuration parameters I am missing.

harun
  • 129
  • 3
  • 11

1 Answers1

4

You need to add a JavaEE module and add create a JavaEE artifact. Then add all libraries you need to that artifact.

Iarwa1N
  • 315
  • 1
  • 9
  • it seems a java ee module is essential to deploy the application. @Iarwa1N , your answer helped except that all other artifacts should be added to java ee artifact. – harun Dec 17 '13 at 09:11
  • @Iarwa1N Could you please elaborate on the steps I need to take in order to get a Maven web project running on Glassfish on IntelliJ IDEA ? – ruwanego Jan 06 '14 at 06:21
  • @iarwa1N may you help me with the following topic http://stackoverflow.com/questions/22969602/nullpointerexception-in-java-ee-application/22970423?noredirect=1#22970423. I didn't succed in deploying correctly my application. May you resolve me the problem? – Mazzy Apr 10 '14 at 11:14