Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: something wrong with my JPA application.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question something wrong with my JPA application.

    Hello everybody! I use glassfish local server and intellij IDEa for developing. i use jpa with eclipselink.
    I'm already three days can't solve problems with my app. from the beginning i can't just add entity to the database. But now i can't even run servlet...
    I have jsp page with button, when i press it servlet have to start.
    here is my index.jsp:
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Start page</title>
    </head>
    <body>
     
    <form action="<%=request.getContextPath()%>/insert" method="get">
        <input type="submit" value="Start"/>
    </form>
     
    </body>
    </html>
    and my servlet:
    package controller;
     
    import beans.SubscriberServiceBean;
     
    import javax.annotation.Resource;
    import javax.ejb.EJB;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.transaction.*;
    import java.io.IOException;
     
     @WebServlet(name = "CS", urlPatterns = {"/insert"})
    public class Controller extends HttpServlet {
     
        @EJB
        private SubscriberServiceBean subscriberBean;
     
        @Resource
        private UserTransaction ut;
     
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
            try {
                resp.getOutputStream().println("before transaction");
     
                ut.begin();
                subscriberBean.createSubscriber("some@mail.com", "some name");
                resp.getOutputStream().println("inside transaction");
                ut.commit();
     
                resp.getOutputStream().println("after transaction");
            } catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
                try {
                    ut.rollback();
                } catch (SystemException e1) {
                    e1.printStackTrace();
                }
                resp.getOutputStream().println(e.getMessage());
            }
     
        }
     
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
        }
    }

    my service interface and ejb bean:
    package beans.interfaces;
     
    import entities.Subscriber;
     
    import javax.ejb.Local;
     
    @Local
    public interface SubscriberService {
        public Subscriber createSubscriber(String email, String name);
     
    }

    package beans;
     
    import beans.interfaces.SubscriberService;
    import entities.Subscriber;
     
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
     
    @Stateless
    public class SubscriberServiceBean implements SubscriberService{
     
        @PersistenceContext(unitName="MS")
        protected EntityManager em;
     
        public EntityManager getEntityManager(){
            return em;
        }
     
        @Override
        public Subscriber createSubscriber(String email, String name) {
            Subscriber subscriber = new Subscriber();
            subscriber.setEmail(email);
            subscriber.setSubscriberName(name);
            getEntityManager().persist(subscriber);
            return subscriber;
        }
    }
    here is my entity:
    package entities;
     
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import java.io.Serializable;
     
    @Entity
    @Table(name = "subscriber", schema = "mail_service")
    public class Subscriber implements Serializable{
     
        private String email;
        private String subscriberName;
     
        @Id
        @Column(name = "EMAIL")
        public String getEmail() {
            return email;
        }
     
        @Column(name = "SUBSCRIBER_NAME")
        public String getSubscriberName() {
            return subscriberName;
        }
     
        public void setSubscriberName(String subscriberName) {
            this.subscriberName = subscriberName;
        }
     
        public void setEmail(String email) {
            this.email = email;
        }
    }
    my persistence.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
     
        <persistence-unit name="MS" transaction-type="JTA">
     
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <jta-data-source>jdbc/_mail</jta-data-source>
            <class>entities.News</class>
            <class>entities.Subscriber</class>
            <properties>
                <!--<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>-->
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                <property name="javax.persistence.jdbc.user" value="root"/>
                <property name="javax.persistence.jdbc.password" value="life"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mail_service"/>
                <property name="eclipselink.logging.level" value="FINE"/>
            </properties>
        </persistence-unit>
    </persistence>

    and my 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>MailService</groupId>
        <artifactId>MailService</artifactId>
        <version>1.0-SNAPSHOT</version>
     
        <packaging>war</packaging>
        <repositories>
            <repository>
                <id>EclipseLink</id>
                <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
            </repository>
        </repositories>
        <dependencies>
     
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.23</version>
                <scope>compile</scope>
            </dependency>
     
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>6.0-SNAPSHOT</version>
                <scope>compile</scope>
            </dependency>
     
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
     
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
            </dependency>
     
            <!--<dependency>-->
                <!--<groupId>org.eclipse.persistence</groupId>-->
                <!--<artifactId>eclipselink</artifactId>-->
                <!--<version>2.5.0</version>-->
                <!--<scope>compile</scope>-->
            <!--</dependency>-->
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.1.0</version>
            </dependency>
            <!--<dependency>-->
                <!--<groupId>org.eclipse.persistence</groupId>-->
                <!--<artifactId>org.eclipse.persistence.core</artifactId>-->
                <!--<version>2.5.0</version>-->
            <!--</dependency>-->
     
            <dependency>
                <groupId>javax.persistence</groupId>
                <artifactId>persistence-api</artifactId>
                <version>1.0.2</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa</artifactId>
                <version>2.5.0</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>eclipselink</artifactId>
                <version>2.5.0</version>
                <scope>compile</scope>
            </dependency>
     
            <!--<dependency>-->
                <!--<groupId>org.hibernate</groupId>-->
                <!--<artifactId>hibernate-core</artifactId>-->
                <!--<version>4.3.0.Beta3</version>-->
            <!--</dependency>-->
     
            <!--<dependency>-->
                <!--<groupId>org.hibernate</groupId>-->
                <!--<artifactId>hibernate-entitymanager</artifactId>-->
                <!--<version>4.3.0.Beta3</version>-->
            <!--</dependency>-->
     
     
        </dependencies>
     
        <build>
            <finalName>mail</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
     
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    now when i deploy application to the glassfish server (through the glassfish console) and enter to the brouser: http://localhost:8080/mail/insert
    i receive errors:
    type Exception report

    message

    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: PWC1392: Error instantiating servlet class controller.Controller
    root cause

    com.sun.enterprise.container.common.spi.util.Injec tionException: Error creating managed object for class: class controller.Controller
    root cause

    com.sun.enterprise.container.common.spi.util.Injec tionException: Exception attempting to inject Remote ejb-ref name=controller.Controller/subscriberBean,Remote 3.x interface =beans.SubscriberServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=beans.SubscriberServiceBean,refType=Session into class controller.Controller: Lookup failed for 'java:comp/env/controller.Controller/subscriberBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming}
    root cause

    javax.naming.NamingException: Lookup failed for 'java:comp/env/controller.Controller/subscriberBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=controller.Controller/subscriberBean,Remote 3.x interface =beans.SubscriberServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=beans.SubscriberServiceBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' [Root exception is javax.naming.NamingException: Lookup failed for 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found]]]
    root cause

    javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=controller.Controller/subscriberBean,Remote 3.x interface =beans.SubscriberServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=beans.SubscriberServiceBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' [Root exception is javax.naming.NamingException: Lookup failed for 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found]]
    root cause

    javax.naming.NamingException: Lookup failed for 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found]
    root cause

    javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found
    note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.
    what's wrong? any suggestions? i'm so tired with solving this problem. so be thankful for any help!


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: something wrong with my JPA application.

    You already given these two codes in your persistence.xml file

    <class>entities.News</class>
            <class>entities.Subscriber</class>

    But i didn't see any thing related to Controller class in this file.I am not familiar with this technology. But i think in hibernate 4 we have to specify all class containing directories for scanning.Not sure.Hope it may help.

Similar Threads

  1. Replies: 0
    Last Post: January 15th, 2013, 08:59 PM
  2. Replies: 16
    Last Post: December 15th, 2012, 06:05 PM
  3. JPA many to many
    By tarkal in forum JDBC & Databases
    Replies: 2
    Last Post: April 22nd, 2012, 09:34 AM
  4. Jpa application
    By sureshJava in forum Member Introductions
    Replies: 0
    Last Post: February 29th, 2012, 02:25 AM
  5. Securing a Java EE6 application with JPA
    By padjester in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 8th, 2010, 10:50 AM