- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为学校练习创建一个小程序。我不知道我哪里做错了。有人可以帮我解决这个问题吗?
运行测试时出现此错误。我在 this.emf = TestUtil.getEMF();
行收到错误。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package herkansing;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries({
@NamedQuery(name = "Account.getAll", query = "select a from Account as a"),
@NamedQuery(name = "Account.count", query = "select count(a) from Account as a"),
@NamedQuery(name = "Account.findByAccountNr", query = "select a from Account as a where a.accountNr = :accountNr")
})
public class Player implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private Long accountNr;
private String email;
private Long money;
private Long points;
public Player() {
}
public Player(Long accountNr){
money = 0L;
points = 0L;
this.accountNr = accountNr;
}
//<editor-fold defaultstate="collapsed" desc="getters and setters ....">
public Boolean add(Long amount) {
if (money + amount >= points) {
money += amount;
return true;
} else {
return false;
}
}
public Long getId() {
return Id;
}
public void setId(Long id) {
this.Id = id;
}
public Long getAccountNr() {
return accountNr;
}
public void setAccountNr(Long nr) {
this.accountNr = nr;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getMoney() {
return money;
}
public void setMoney(Long money) {
this.money = money;
}
public Long getPoints() {
return points;
}
public void setPoints(Long points) {
this.points = points;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Player other = (Player) obj;
if (this.accountNr != other.accountNr && (this.accountNr == null || !this.accountNr.equals(other.accountNr))) {
return false;
}
if (this.money != other.money && (this.money == null || !this.money.equals(other.money))) {
return false;
}
if (this.points != other.points && (this.points == null || !this.points.equals(other.points))) {
return false;
}
return true;
}
}
玩家等级
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="playerPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>herkansing.Player</class>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?user=root"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<!--adds logging-->
<property name="eclipselink.logging.logger" value="DefaultLogger"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
持久性.xml
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package herkansing;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
*
* @author Bart
*/
public class TestUtil {
static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("playerPU");
/**
*
* @return the number of Accounts stored in the database
*/
static public int getNrOfAccountRecordsInDB() {
EntityManager em = getEMF().createEntityManager();
return ((Number) em.createNamedQuery("Player.count").getSingleResult()).intValue();
}
/**
* Search for an entity of the specified class and primary key.
*
* @param id
* @return the found Account instance or null if the entity does not exist
*/
static public Player getAccountById(Long id) {
EntityManager em = getEMF().createEntityManager();
return em.find(Player.class, id);
}
static public EntityManagerFactory getEMF() {
return emf;
}
}
TestUtil 类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package herkansingTest;
import herkansing.TestUtil;
import herkansing.DatabaseCleaner;
import herkansing.Player;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Bart
*/
public class PlayerTest {
final EntityManagerFactory emf;
EntityManager em, em1, em2;
private static final Logger LOG = Logger.getLogger(PlayerTest.class.getName());
public PlayerTest() {
this.emf = TestUtil.getEMF();
}
@Before
public void setUp() {
em = emf.createEntityManager();
em1 = emf.createEntityManager();
em2 = emf.createEntityManager();
new DatabaseCleaner().resetDatabase();
}
}
玩家测试
java.lang.ExceptionInInitializerError
at herkansingTest.PlayerTest.<init>(PlayerTest.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [playerPU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select a from Account as a].
[14, 21] The abstract schema type 'Account' is unknown.
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:866)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:806)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:183)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:528)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:146)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:183)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at herkansing.TestUtil.<clinit>(TestUtil.java:18)
... 31 more
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [playerPU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select a from Account as a].
[14, 21] The abstract schema type 'Account' is unknown.
at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:239)
... 41 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select a from Account as a].
[14, 21] The abstract schema type 'Account' is unknown.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.JPAQuery.processJPQLQuery(JPAQuery.java:223)
at org.eclipse.persistence.internal.jpa.JPAQuery.prepare(JPAQuery.java:184)
at org.eclipse.persistence.queries.DatabaseQuery.prepareInternal(DatabaseQuery.java:624)
at org.eclipse.persistence.internal.sessions.AbstractSession.processJPAQuery(AbstractSession.java:4363)
at org.eclipse.persistence.internal.sessions.AbstractSession.processJPAQueries(AbstractSession.java:4323)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:584)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:804)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:748)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:253)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:728)
... 39 more
堆栈跟踪
最佳答案
您可以在此处的堆栈跟踪中查看原因:
Exception Description: Problem compiling [select a from Account as a].
[14, 21] The abstract schema type 'Account' is unknown.
JPA 无法找到 Account
实体,并且无法创建 EntityManagerFactory
。
您必须列出 persistence.xml
中的所有实体,就像列出 Player
类一样:
<class>herkansing.Player</class>
<class>herkansing.Account</class>
...
即使您没有在代码中显式使用 Account
实体,也会有一个引用它的命名查询,并且 JPA 会尝试验证此查询并给出异常:
@NamedQueries({
@NamedQuery(name = "Account.getAll", query = "select a from Account as a"),
@NamedQuery(name = "Account.count", query = "select count(a) from Account as a"),
@NamedQuery(name = "Account.findByAccountNr", query = "select a from Account as a where a.accountNr = :accountNr")
})
或者,您可以删除 @NamedQueries
声明。
关于java - 获取 java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34945152/
我在项目的不同领域遇到了以下异常。最糟糕的是我不知道它是什么......我的项目中没有 com.b.a.c.b 包。我尝试在网络上搜索,但仍然不明白是什么导致了此异常。 在从另一个 Activity
当我尝试单独运行 junit 测试时,出现此错误。当我尝试使用 Ant(它运行所有测试)运行它时,它运行良好。谁能告诉我可能的原因是什么? src 文件夹和 test 文件夹在同一层次结构中。我必须链
考虑以下类: sealed class Fruit(val id: String, val label: String) { object orange : Fruit("Citrus sinen
编译良好: static final Screen screen = Screen.getInstance(); static final InputListener listener = Input
我正在尝试读取 jar 包中的文件 (blip3.out)。我正在使用 getResourceAsStream 获取 url,然后尝试从中读取。我已经进行了多次尝试,并使用其他帖子中的解决方案,但仍然
我将一个项目导入到 eclipse 中并添加了依赖项(仅 oracle 驱动程序和 junit4)。但是当我尝试运行该项目时,我收到 ExceptionInInitializerError 。由于某种
当我尝试运行以下代码时,我收到 ExceptionInInitializerError 而不是空指针异常。为什么? static { String s= null; System.out.
我正在尝试使用 Class.forName 从我的对象调用方法,但出现 java.lang.ExceptionInInitializerError class MainClass(implicit v
我收到了ExceptionInInitializerError。我被告知“getException() 方法现在被称为原因,并且可以通过 Throwable.getCause() 方法以及前面提到的“
我是 Applet 编程新手,想要制作一个 Applet 放在网站上。那么我们开始吧。 这个项目的目标是,如果您单击该按钮,它将在浏览器顶部打开一个 JFrame。但在测试时,它给了我一个 java.
我使用 swing JFrame 作为我的应用程序的 MainFrame。我有一个按钮应该创建一个新窗口。但它会在初始化时崩溃,我不知道为什么。 public class Dialog { p
下面是我在运行服务器时从 netbeans IDE 得到的错误。正如错误所说 Uncompilable source code - Projects.ApplicationMenu is not ab
我正在使用兼容性类来构建用户代理字符串: public abstract class Compatibility { private static int sdkInt = 0; pr
编辑:已解决,但我不明白为什么 在 PokemonEnum 中我有这行 private PokemonEnum[ ] pokemon = PokemonEnum.values(); 我把它改为: pr
我正在尝试初始化 GL11,因为我在引用一个有 的方法时遇到了麻烦 GL11 gl 作为它的参数。我试图在我的渲染器类中初始化它,但它没有用,所以我认为它的初始化搞乱了渲染器并创建了一个新类来初始化。
我在一些论坛上搜索过,这似乎是一个常见问题。但是我找不到解决方案。我没有做任何疯狂的事情,所以发生这种情况似乎很奇怪。 @Override protected Scene onCreateScene(
我一定是在做一些非常愚蠢的事情,但是当我尝试在我的单例中实例化一个对象时,我得到了一个 ExceptionInInitializerError: class MySingleton { priva
请注意,我知道关于的规则 ExceptionInInitializerErrors 它说:任何静态 block 中抛出的异常被包装到ExceptionInInitializerError 然后抛出 E
我在尝试运行我的应用程序时意外遇到了 ExceptionInIntiialize 错误。我相信当用户点击快速聊天按钮时会提示错误。 10-09 18:27:08.450: E/AndroidRunti
当启动 JavaFX 应用程序(嵌入了 Swing 代码)作为 Webstart 时,会发生以下错误: java.lang.ExceptionInInitializerError at com
我是一名优秀的程序员,十分优秀!