- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的 DAO 对象创建 JUnit 测试。 JUnit 直接从 Netbeans 运行。我对其中一些技术还很陌生,因此我花了很多时间来追踪我的错误来自哪里。我的代码和我看到的相应错误是:
Jul 22, 2011 7:09:09 PM com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default. FATAL [DatasourceConnectionProvider] - Could not find datasource: Waylon org.omg.CORBA.COMM_FAILURE: FINE: IOP00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700 vmcid: OMG minor code: 1 completed: No javax.naming.NamingException: Lookup failed for 'Waylon' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: IOP00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700 vmcid: OMG minor code: 1 completed: No]] at sun.reflect.GeneratedConstructorAccessor31.newInstance(Unknown Source) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at javax.naming.InitialContext.lookup(InitialContext.java:392) at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:248) at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:95) at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52) at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.handleFullLogging(WrapperGenerator.java:387) at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124) at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.access$400(WrapperGenerator.java:107) at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29) at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator$2.invoke(WrapperGenerator.java:511) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62) at com.sun.corba.ee.spi.orbutil.proxy.CompositeInvocationHandlerImpl.invoke(CompositeInvocationHandlerImpl.java:99) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009) at $Proxy40.connectFailure(Unknown Source) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292) at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:257) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859) at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:270) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:129) at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:223) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126) at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:228) at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:393) at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112) at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69) at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94) at com.sun.enterprise.naming.impl.SerialContext$ProviderCacheKey.getNameService(SerialContext.java:1241) at waylon.label.LabelDAOIJTest.setUpClass(LabelDAOIJTest.java:36) at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:411) at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at javax.naming.InitialContext.lookup(InitialContext.java:392) at java.lang.reflect.Method.invoke(Method.java:597) at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859) at org.junit.runners.ParentRunner.run(ParentRunner.java:303) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518) at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906) at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94) at waylon.label.LabelDAOIJTest.setUpClass(LabelDAOIJTest.java:36) Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: IOP00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700 vmcid: OMG minor code: 1 completed: No] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:352) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504) ... 29 more at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="WaylonPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>Waylon</jta-data-source>
<class>waylon.label.Label</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
我正在测试的 DAO:
package waylon.label.impl;
import com.google.inject.Inject;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import waylon.label.Label;
import waylon.label.LabelDAO;
/**
* {@inheritDoc}
*/
public class LabelDAOImpl implements LabelDAO {
private final EntityManager em;
@Inject
public LabelDAOImpl( EntityManager em ) {
this.em = em;
}
/**
* {@inheritDoc}
*/
@Override
public List<Label> getAllLabels() throws Exception {
TypedQuery<Label> typedQuery = em.createQuery(
"SELECT * from LABEL", Label.class );
return typedQuery.getResultList();
}
/**
* {@inheritDoc}
*/
@Override
public Label createLabel(String name) throws Exception {
em.getTransaction().begin();
Label label = new Label();
label.setName( name );
em.persist( label );
em.getTransaction().commit();
return label;
}
/**
* {@inheritDoc}
*/
@Override
public void removeLabel(String name) throws Exception {
Label label = getLabel( name );
removeLabel( label );
}
/**
* {@inheritDoc}
*/
@Override
public Label getLabel(String name) throws Exception {
Label label = em.find( Label.class, name );
return label;
}
/**
* {@inheritDoc}
*/
@Override
public void removeLabel(Label label) throws Exception {
if ( null != label ) {
em.getTransaction().begin();
em.remove( label );
em.getTransaction().commit();
}
}
}
我的 JUnit 测试:
package waylon.label;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.PersistService;
import com.google.inject.persist.jpa.JpaPersistModule;
import java.util.List;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import org.hamcrest.core.IsEqual;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import waylon.label.impl.LabelDAOModule;
/**
* A JUnit Test to test our database connection and JPA code.
* @author Benjamin Bays
*/
public class LabelDAOIJTest {
private LabelDAO objectInTest = null;
private static final String PUNIT = "WaylonPU";
private static Injector injector = null;
private static PersistService persistService = null;
@BeforeClass
public static void setUpClass() throws Exception {
injector = Guice.createInjector(
new JpaPersistModule(PUNIT),
new LabelDAOModule() );
persistService = injector.getInstance( PersistService.class );
persistService.start();
}
@AfterClass
public static void tearDownClass() throws Exception {
persistService.stop();
}
@Before
public void setUp() {
objectInTest = injector.getInstance( LabelDAO.class );
}
@Test
public void doEverything() throws Exception {
final String testName = "Test Label";
Label label1 = objectInTest.createLabel( testName );
assertThat( label1.getName(), IsEqual.equalTo( testName ) );
Label label2 = objectInTest.getLabel(testName);
assertThat( label2.getName(), IsEqual.equalTo( testName ) );
final String testName3 = "Test Label3";
Label label3 = objectInTest.createLabel( testName3 );
assertThat( label3.getName(), IsEqual.equalTo( testName3 ) );
List<Label> allLabels = objectInTest.getAllLabels();
assertThat( allLabels, hasItems( label1, label3 ) );
objectInTest.removeLabel( label1 );
objectInTest.removeLabel( testName3 );
List<Label> noLabels = objectInTest.getAllLabels();
assertThat( noLabels.size(), IsEqual.equalTo( 0 ) );
}
}
任何帮助将不胜感激。谢谢。
最佳答案
这个问题花了很长时间才弄清楚。我认为这里的总体教训是一次只学习一项新技术。我提供了运行 JUnit 所采取的一系列步骤,但总的来说,RTFM 解决了几个错误。
为了给予应得的荣誉,这些资源发挥了巨大的作用。
我首先使用第一个资源重新创建数据库服务。这涉及到重新创建每个表并重新建立外键依赖关系(很烦人,但很有必要)。
然后,我按照第二个教程让 netbeans 自动从我的数据库表生成一个实体。我的实体不需要任何更改,但这确实导致 Netbeans 生成 persistence.xml。我将它与我自己的进行比较,并提出了这个片段:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="LabelDAOIJTest" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>waylon.label.Label</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:Waylon;create=true"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
这里的重大变化是:
终于,我的单元测试运行了,万岁!为了解决这个问题,我的生产代码中存在错误(因为我是 JPA 的初学者)。我所做的更改是针对 LabelDAOImpl.java。我了解到 SELECT * 在 JPA 中不起作用:)
/**
* {@inheritDoc}
*/
@Override
public List<Label> getAllLabels() throws Exception {
TypedQuery<Label> typedQuery = em.createQuery(
"SELECT x FROM Label x", Label.class );
return typedQuery.getResultList();
}
关于java - 运行 JUnit 测试时出错(JPA、Derby、Hibernate、Guice),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6792866/
我有以下情况要解决,但无法正常工作(尝试了Hibernate和EclipseLink): Table_1: Column_A is Primary Key ... some other
我是 JPA 的新手,但必须在该技术中实现我的项目 我想做的是通过 CriteriaQuery 构建一些数据库查询,但不知道如何将参数列表传递给下面的代码: CriteriaBuilder qb =
我是 JPA 新手,注意到可以通过使用 @Version 注释实体中的字段来使用乐观锁定。我只是好奇,如果之前不存在,持久性提供程序是否会创建一个隐式版本字段。例如网站objectdb状态: "Whe
我有一个 JPA 查询 @Query(value = "SELECT SUM(total_price) FROM ... WHERE ...", nativeQuery = true) 当有匹配的记录
JPA 是否会尝试在已经持久(和非分离)的实体上级联持久化? 为了清楚起见,这是我的情况:我想保留一个新用户: public void addUser(){ //User is an enti
显然,OpenJPA。我也看到提到过 EclipseLink 和 Hibernate,但是在功能上有显着差异吗? 最佳答案 大多数差异来自提供者对 OSGi 的感知程度。例如,您可能需要自己将 Hib
我想将 JPA 用于 micronaut。为此,我使用 io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 库。每当我运行应用程序并点击端点
我正准备为我的应用实现后端,现在我正在投影数据层。我期待着 Spring 。 最佳答案 Spring Data JPA 不是 JPA 实现。它提供了将数据访问层构建到底层 JPA 顶部的方法。您是否应
假设我有一个表 Item,其中包含一个名为 user_id 的列和一个表 User 以及另一个名为 Superuser 的列: CREATE TABLE Item(id int, user_id in
JPA 2.1 规范说: The entity class must not be final. No methods or persistent instance variables of the
我正在从事一个具有一些不寻常实体关系的项目,我在使用 JPA 时遇到了问题。有两个相关对象;用户,让我们称另一个 X。用户与 X 具有一对多和两个一对一的关系。它基本上看起来像这样 [用户实体] @O
我说的是 JavaEE 中的 JPA。在我读过的一本书中谈到: EntityManager em; em.find(Employee.class, id); “这是实体管理器在数据库中查找实例所需的所
我有 JPA 支持的 Vaadin 应用程序。此应用程序中的组件绑定(bind)到 bean 属性(通过独立的 EL 实现)。一些组件绑定(bind)到外部对象(或其字段),由@OneToOne、@O
是否可以使表中的外键唯一?假设我有实体 A 和 B。 答: @Entity class A extends Serializable { @Id private long id; @OneToOne
我在使用 JPA 时遇到了一点问题。考虑这种情况: 表 A (id_a) | 表 B (id_b, id_a) 我需要的是这样的查询: Select a.*, c.quantity from A as
我有一个由 JPA 管理的实体类,我有一个实体需要在其属性中记录更改。 JPA 是否提供任何方法来处理这种需求? 最佳答案 如果您使用 Hibernate 作为 JPA 提供程序,请查看 Hibern
我想实现以下架构: Table A: a_id (other columns) Table B: b_id (other columns) Table C: c_id (other columns)
我有一个愚蠢的问题。如果能做到的话那就太好了,但我并没有屏住呼吸。 我需要链接到我的 JPA 实体的表中的单个列作为所述 JPA 实体中的集合。有什么方法可以让我单独取回与该实体相关的列,而不必取回整
我有一个 Open JPA 实体,它成功连接了多对多关系。现在我成功地获取了整个表,但我实际上只想要该表中的 ID。我计划稍后调用数据库来重建我需要的实体(根据我的程序流程)。我只需要 ID(或该表中
这是我的一个实体的复合主键。 public class GroupMembershipPK implements Serializable{ private static final long
我是一名优秀的程序员,十分优秀!