0
votes

All the code below resolves to one simple em.persist(entity) call that fails, when everything I read says this should work. Everything behaves as expected until I call em.persist().

Can anyone see where I'm going wrong?

My database is MySql.
The relevant MySql database is "godb".
It contains a table named "loggedins". I used PK INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY to Create the table's primary key.

I used the Glassfish Admin Console to:

Configure a Glassfish JDBC Connection Pool named "GoSQLPool". I gave it additional properties so that when I PING it I get "Ping Successful".

And I Configured a JDBC Resource named "jdbc/go". It uses "GoSqlPool". Its JNDI name is "jdbc/go"

This is my persistance.xml file:

<persistence 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" version="2.0">
  <persistence-unit name ="goDataBase">
    <jta-data-source>jdbc/go</jta-data-source>
  </persistence-unit>
</persistence>

persistence.xml is deployed in an ear package, which is where I read it's supposed to be:

go.ear
    META-INF
        application.xml
        persistence.xml
    go.war
    go-server.jar

My entity bean is annotated as follows:

@Entity(name="LoggedIn")
@Table(name="loggedins")
public class LoggedIn extends EntityParent implements Serializable, DataItemsValues {
  //constructors
  public LoggedIn() {
    this.id   = ""; this.pw   = ""; this.hash = "";
  }
  public LoggedIn(String id, String pw, String hash) {
    this.id = id; this.pw = pw;    this.hash = hash;
  }

In case it matters, EntityParent is just an abstract class that reminds me I need to have get id, pw and hash methods in all entities whether they are for persisted fields or transient ones.

public abstract class EntityParent {
  abstract public String getId ();
  abstract public String getPw ();
  abstract public String getHash();
}

My stateless java bean is annotated as follows:

@Stateless
public class LoggerBean {
  @PersistenceContext(unitName = "goDataBase")
  private EntityManager em;

This is the method in LoggerBean that throws the exception:

private Integer login (LoggedIn entity, String hash, String id, 
                         String pw, StringBuffer sb)
  throws GoExceptionServer {
    U.upMarginS();
    String iAm = U.getIAmS (Thread.currentThread ().getStackTrace ());
    sb.append("\r\n" + iAm + "beg");

    try {
      sb.append("\r\n  " + iAm + "persiting entity: " + entity);
///////////THIS IS THE ONLY NON PRINTING CODE///////////////////
      this.em.persist(entity);

      sb.append("\r\n" + iAm + "end");
      U.downMarginS();
      return null;
    } catch (Exception e) {
      sb.append("\r\n  " + iAm +  "caught: " + e.getClass().getName());
      sb.append("\r\n  " + iAm +  "msg:    " + e.getMessage());
      sb.append("\r\n  " + iAm +  "cause : " + e.getCause());
      sb.append("\r\n  " + iAm +  "Throwing a GoExceptionServer.");
      sb.append("\r\n" + iAm +  "end");
      U.downMarginS();
      throw new GoExceptionServer(MessageValues.MSG_KEY_UNEXPECTED_EXCEPTION);
    }

This is the log output from the above system.out statements:

LoggerBean.login.............................beg
      LoggerBean.login.............................persisting entity: LoggedInBean: PK(null) MBR_ID(G) MBR_PW(G)
      LoggerBean.login.............................caught: java.lang.IllegalStateException
      LoggerBean.login.............................msg:    Unable to retrieve EntityManagerFactory for unitName goDataBase
      LoggerBean.login.............................cause : null
      LoggerBean.login.............................Throwing a GoExceptionServer.
    LoggerBean.login.............................end

I'm lost. Can anyone suggest a way to make this work? The fact that the LoggedIn entity has a null PK (primary key field) bothers me, but since the table auto-generates them I can't assign one before it is persisted.

Any suggestions or corrections are appreciated.

1

1 Answers

1
votes

Everything seems to be OK, the error is that it is not possible to find configuration for goDataBase persistence unit. The file persistence.xml should be in folder META-INF, but according to your listing, the folder is named META_INF, which is not correct.