entitymanager

After using the unwrap method on entitymanager to get the native hibernate session do I have to close both?

时光毁灭记忆、已成空白 提交于 2019-12-07 04:10:15
问题 I have code that looks like this. this.entityManager = AppFactory.instance().getEntityManagerFactory().createEntityManager(); this.hibernateSession = entityManager.unwrap(Session.class); try{ //do some queries using both entityManager and hibernateSession }finally{ this.entityManager.close(); } But I seem to have a connection leak somewhere. I'm wondering if I am supposed to close both entityManager and hibernateSession. Has anybody else worked with this type of situation? 回答1: You do not

Symfony2 subquery within Doctrine entity manager

自闭症网瘾萝莉.ら 提交于 2019-12-07 03:22:36
问题 I need to perform this query: SELECT * FROM (SELECT * FROM product WHERE car = 'large' ORDER BY onSale DESC) AS product_ordered GROUP BY type In Symfony2 using the entity manager. My basic query builder would be : $query = $em->getRepository('AutomotiveBundle:Car') ->createQueryBuilder('p') ->where('pr.car = ?1') ->andWhere('pr.status = 1') ->orderBy('pr.onSale', 'DESC') ->setParameter(1, $product->getName()) ->groupBy('p.type') ->getQuery(); But I cannot work out how to add in a subquery to

Doctrine :FetchAll() with limits

僤鯓⒐⒋嵵緔 提交于 2019-12-06 18:33:00
问题 I want to make a fetchAll() with limit ? Do you know if it's possible with the entity manager of symfony2 ? My current code (Fetch all, without limit): $repository = $this->getDoctrine()->getRepository('MyBundle:Download'); $product = $repository->findAll(); Thanks you all. Best regards, EDIT: $em = $this->getDoctrine()->getRepository('MyBundle:Download'); $ouput = $em->findBy(array(), array('id' => 'DESC'),5); Return the 5 last rows. Thanks all. 回答1: It's often instructive to check the

JTA EntityManager cannot use getTransaction() [Spring + Hibernate + EntityManager]

徘徊边缘 提交于 2019-12-06 14:08:08
I am using Spring + JPA + Hibernate + EntityManager to talk to the database. I am getting 'A JTA EntityManager cannot use getTransaction() ' error. Please provide your insights and help me resolve the issue. beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" ... xmlns definitions... xsi:schemaLocation="..."> <context:component-scan base-package="com.mycompany.myproject" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" /> <bean class="org.springframework.orm.jpa.support

TestNG: Identifying which tests methods are next

ε祈祈猫儿з 提交于 2019-12-06 12:11:29
My goal is to clear() my javax.persistence.EntityManager after each test method. Here's an example of a test class: public class Example { @Test(dataProvider = "sampleDataProvider") public void testA(String parameter) { System.out.println(parameter); } @Test(dataProvider = "sampleDataProvider") public void testB(String parameter) { System.out.println(parameter); } } The entityManager is used in the dataProvider "sampleDataProvider" by querying the DB for test data which is then compiled in this format: new Object[2][1] . Keep in mind that the querying and compiling of data is all done before a

JPA update list of one to many relationship

心已入冬 提交于 2019-12-06 08:49:15
问题 I have a Question entity with a list of another entity called Alternatives like this: public class Question { @OneToMany(fetch = FetchType.LAZY, mappedBy = "question", cascade = CascadeType.ALL) @JsonManagedReference private List<Alternative> alternativeList; } public class Alternative implements Serializable { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "questionId", nullable = false) @JsonBackReference private Question question; } Then I wanted to update an exiting Question entry

how to setup spring data jpa with multiple datasources

青春壹個敷衍的年華 提交于 2019-12-06 07:12:16
问题 I am using Spring Data Jpa version 1.0.0.M2 here is the url: http://static.springsource.org/spring-data/data-jpa/docs/1.0.0.M2/reference/pdf/spring-data-jpa-reference.pdf All is promised to be very simple and nice, but when it comes to two datasources it breaks down. The question is how to setup with two data sources? The JpaRepository automatically searches for EntityManager, when it finds more than two it throws exceptions. If you have any idea with EntityManager and how to setup the spring

New EntityManager sometimes getting stale data from MySQL

送分小仙女□ 提交于 2019-12-06 06:16:47
JPA; Hibernate 4.3.6; MySQL 6.2 (InnoDB); Vaadin web application running on Tomcat When I read an entity from the database I sometimes get stale data. I can’t find the pattern— sometimes the same code pulls stale data, then clean data, then stale again. I am getting a new EntityManager before each query. (I was concerned that I might be somehow getting them from a pool of reused EntityManagers, but flushing the “new” EntityManager gives me an error that no transaction is in progress.) Hibernate's debug log, and stepping through the code, both indicate that it is hitting the database each time.

JPA EntityManager caching

為{幸葍}努か 提交于 2019-12-06 05:47:50
I have an entity defined as follows: public class Version { @Id private Long id; private String content; @Transient private Model model; //... } From what I can see, when a find operation is done on Entity Manager, it makes a SELECT on the underlying database only once, and then the entity is cached in the Entity Manager. However, I see that if I assign a Model to the model property, this change is not reflected to the cached entity. E.g. if in one call, a find operation is done and Model is assigned, when I do find again from another EJB, model property is null again. Is this change not

Transaction required exception on execute update for JPQL update query

巧了我就是萌 提交于 2019-12-06 02:54:24
问题 I get this error when I try to run this code. Error: javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager Code: (_ut is a UserTransaction object) public void setMainCategory(Integer deptId, Integer catId) { try { Query setmain = _entityManager.createNamedQuery("Category.setAsMain"); Query removeMain = _entityManager.createNamedQuery("Category.removeMain");