无状态 Bean 不能持久化简单实体
Stateless Bean Cannot Persist Simple Entity
下面的所有代码都解析为一个失败的简单 em.persist(entity) 调用,而我阅读的所有内容都表明这应该有效。在我调用 em.persist() 之前,一切都按预期运行。
谁能看出我错在哪里?
我的数据库是 MySql.
相关的 MySql 数据库是 "godb".
它包含一个名为 "loggedins" 的 table。
我使用 PK INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY 来创建 table 的主键。
我使用 Glassfish 管理控制台来:
配置名为 "GoSQLPool" 的 Glassfish JDBC 连接池。我给了它额外的属性,所以当我 PING 它时,我得到 "Ping Successful".
并且我配置了一个名为 "jdbc/go" 的 JDBC 资源。它使用 "GoSqlPool"。它的 JNDI 名称是 "jdbc/go"
这是我的 persistance.xml 文件:
<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部署在一个ear包里,我看应该是:
go.ear
META-INF
application.xml
persistence.xml
go.war
go-server.jar
我的实体bean注释如下:
@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;
}
以防万一,EntityParent 只是一个抽象 class,提醒我我需要在所有实体中使用 get id、pw 和 hash 方法,无论它们是用于持久字段还是瞬态字段。
public abstract class EntityParent {
abstract public String getId ();
abstract public String getPw ();
abstract public String getHash();
}
我的无状态java bean注释如下:
@Stateless
public class LoggerBean {
@PersistenceContext(unitName = "goDataBase")
private EntityManager em;
这是LoggerBean中抛出异常的方法:
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);
}
这是上述 system.out 语句的日志输出:
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
我迷路了。任何人都可以建议一种方法来完成这项工作吗?
LoggedIn 实体有一个空 PK(主键字段)这一事实困扰着我,但由于 table 自动生成它们,我无法在它被持久化之前分配一个。
如有任何建议或更正,我们将不胜感激。
一切似乎都正常,错误是无法找到 goDataBase
持久性单元的配置。文件 persistence.xml 应该在文件夹 META-INF 中,但根据您的清单,该文件夹名为 META_INF,这是不正确的。
下面的所有代码都解析为一个失败的简单 em.persist(entity) 调用,而我阅读的所有内容都表明这应该有效。在我调用 em.persist() 之前,一切都按预期运行。
谁能看出我错在哪里?
我的数据库是 MySql.
相关的 MySql 数据库是 "godb".
它包含一个名为 "loggedins" 的 table。
我使用 PK INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY 来创建 table 的主键。
我使用 Glassfish 管理控制台来:
配置名为 "GoSQLPool" 的 Glassfish JDBC 连接池。我给了它额外的属性,所以当我 PING 它时,我得到 "Ping Successful".
并且我配置了一个名为 "jdbc/go" 的 JDBC 资源。它使用 "GoSqlPool"。它的 JNDI 名称是 "jdbc/go"
这是我的 persistance.xml 文件:
<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部署在一个ear包里,我看应该是:
go.ear
META-INF
application.xml
persistence.xml
go.war
go-server.jar
我的实体bean注释如下:
@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;
}
以防万一,EntityParent 只是一个抽象 class,提醒我我需要在所有实体中使用 get id、pw 和 hash 方法,无论它们是用于持久字段还是瞬态字段。
public abstract class EntityParent {
abstract public String getId ();
abstract public String getPw ();
abstract public String getHash();
}
我的无状态java bean注释如下:
@Stateless
public class LoggerBean {
@PersistenceContext(unitName = "goDataBase")
private EntityManager em;
这是LoggerBean中抛出异常的方法:
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);
}
这是上述 system.out 语句的日志输出:
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
我迷路了。任何人都可以建议一种方法来完成这项工作吗? LoggedIn 实体有一个空 PK(主键字段)这一事实困扰着我,但由于 table 自动生成它们,我无法在它被持久化之前分配一个。
如有任何建议或更正,我们将不胜感激。
一切似乎都正常,错误是无法找到 goDataBase
持久性单元的配置。文件 persistence.xml 应该在文件夹 META-INF 中,但根据您的清单,该文件夹名为 META_INF,这是不正确的。