我们使用Guice Persist在我们的项目中注入EntityManager.
例如
public class MyDao{
@Inject
EntityManager em;
public void someMethod(){
//uses em instance
}
}
Run Code Online (Sandbox Code Playgroud)
但我们还不清楚如何使用注入的实例EntityManager.
有人可以告诉我JPA 1.0 EntityManager在通过find检索对象时的内在原因,如果找不到则必须处理null,但是当通过createQuery使用Query接口时getResultList会在找不到时抛出NoResultException.
也许我错过了一些东西,但我觉得它与语言非常不一致,实际上我不得不做很多的重新设计,因为使用查询界面从简单的查找器更改为更细粒度的查询.
多谢你们.
我需要处理一个CSV文件,并且每个记录(行)都会保留一个实体.现在,我这样做:
while ((line = reader.readNext()) != null) {
Entity entity = createEntityObject(line);
entityManager.save(entity);
i++;
}
Run Code Online (Sandbox Code Playgroud)
这个save(Entity)方法基本上只是一个EntityManager.merge()电话.CSV文件中大约有20,000个实体(行).这是一种有效的方法吗?这似乎很慢.使用会更好EntityManager.persist()吗?这种解决方案有何缺陷?
编辑
这是一个漫长的过程(超过400s),我尝试了两种解决方案,用persist和merge.两者的完成时间大致相同(459s vs 443s).问题是如果逐个保存实体是最佳的.据我所知,Hibernate(我的JPA提供程序)确实实现了一些缓存/刷新功能,所以我不必担心这一点.
是Silex框架的新手.我想使用doctrine实体管理器进行简单的SQL DB连接.请举一些简单的例子.
在此先感谢
SK
我如何获得自定义对象列表,如查询下面的结果:
SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id
Run Code Online (Sandbox Code Playgroud)
举例:
return getEntityManager().createQuery("SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id").setParameter("id", id).getResultList();
Run Code Online (Sandbox Code Playgroud)
我需要一张类别ID和类别中的产品数量的地图.
在我们的应用程序中,我们希望使用Spring和JPA设置多个数据源.因此我们创建了2个entityManagerFactory,2个数据源和2个事务管理器.
web.xml中
<param-value>
/WEB-INF/a_spring.xml
/WEB-INF/b_spring.xml
</param-value>
Run Code Online (Sandbox Code Playgroud)
persistence.xml中
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="db1" transaction-type="RESOURCE_LOCAL">
<class>com.rh.domain.RcA</class>
</persistence-unit>
<persistence-unit name="db2" transaction-type="RESOURCE_LOCAL">
<class>com.rh.domain.Rcb</class>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
a_spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id = "RcMaintenanceService" class="com.rh.services.RcAbcMaintenanceServiceImpl" autowire="byName" />
<aop:config>
<aop:pointcut id="rOperation" expression="execution(* com.rh.services.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="rOperation"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/db1" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property …Run Code Online (Sandbox Code Playgroud) 目前我正在学习如何使用Symfony2.我到了解他们如何使用Doctrine的地步.
在给出的示例中,他们有时使用实体管理器:
$em = $this->getDoctrine()->getEntityManager();
$products = $em->getRepository('AcmeStoreBundle:Product')
->findAllOrderedByName();
Run Code Online (Sandbox Code Playgroud)
在其他示例中,不使用实体管理器:
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
Run Code Online (Sandbox Code Playgroud)
所以我实际上尝试了第一个例子而没有获得实体管理器:
$repository = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product');
$products = $repository->findAllOrderedByName();
Run Code Online (Sandbox Code Playgroud)
并得到了相同的结果.
那么我什么时候才真正需要实体管理器?什么时候可以立即去存储库呢?
我试图在我的数据库中插入数据,我在我的项目中使用JPA.
这就是我的bean的样子.
@PersistenceContext
EntityManager em;
em.createNativeQuery("INSERT INTO testtable ('column1','column2') VALUES ('test1','test2')").executeUpdate();
Run Code Online (Sandbox Code Playgroud)
myfacade:
@Stateless
public class TestFacade extends AbstractFacade<Test> {
@PersistenceContext(unitName = "TEST2PU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public TestFacade() {
super(Test.class);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager
Run Code Online (Sandbox Code Playgroud)
如果我不使用 @PersistenceContext for EntityManager
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TEST2PU");
EntityManager em = emf.createEntityManager();
em.createNativeQuery("INSERT INTO testtable ('column1','column2') VALUES ('test1','test2')").executeUpdate();
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
javax.persistence.TransactionRequiredException:
Exception Description: No …Run Code Online (Sandbox Code Playgroud) 我想制作一个带限制的fetchAll()?你知道symfony2的实体经理是否有可能?
我当前的代码(全部获取,无限制):
$repository = $this->getDoctrine()->getRepository('MyBundle:Download');
$product = $repository->findAll();
Run Code Online (Sandbox Code Playgroud)
谢谢大家.最好的祝福,
编辑:
$em = $this->getDoctrine()->getRepository('MyBundle:Download');
$ouput = $em->findBy(array(), array('id' => 'DESC'),5);
Run Code Online (Sandbox Code Playgroud)
返回最后5行.
谢谢大家.
我正在尝试将分页与EntityManager.createNativeQuery(). 下面是我正在使用的骨架代码:
var query = em.createNativeQuery("select distinct id from ... group by ... having ...");
List<BigDecimal> results = query
.setMaxResults(pageSize)
.setFirstResult(pageNumber * pageSize)
.getResultList();
Run Code Online (Sandbox Code Playgroud)
当pageNumber是 0(第一页)时,我得到预期的 BigDecimals 列表:
但是一旦pageNumber> 0(例如,第二页),我就会得到一个对象列表,这个列表中的每个对象似乎都包含两个 BigDecimal,其中第一个包含来自数据库的值,第二个 BigDecimal 似乎是这一行的位置。
显然我得到了这个例外
java.lang.ClassCastException: 类 [Ljava.lang.Object; 不能强制转换为 java.math.BigDecimal 类
有人可以解释这种差异,以及如何修复它以始终返回 BigDecimals 列表?谢谢你。
更新 1:我创建了一个示例项目来重现此问题。我只能使用 Oracle 数据库重现此问题。使用 H2 数据库,它运行良好,并且我始终得到一个与页码无关的 BigDecimals 列表。
entitymanager ×10
jpa ×6
java ×4
doctrine-orm ×3
hibernate ×2
symfony ×2
guice ×1
jndi ×1
jpa-2.0 ×1
oracle ×1
pagination ×1
silex ×1
spring ×1