entitymanager

Spring + Hibernate + JPA [closed]

佐手、 提交于 2019-11-30 11:21:35
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . As of now I have a working Spring application with persistence. However now I want to use Hibernate with JPA to do all of my database

How to inject EntityManager in CDI (weld)?

南楼画角 提交于 2019-11-30 08:49:02
In my project , I use JSF+JPA+CDI+WildFly 8.2 in the persistence layer. I have a BasicDao , like this: public class BasicDao<M, K> { private org.jboss.logging.Logger logger = org.jboss.logging.Logger .getLogger("BasicDao"); @Inject @Primary protected EntityManager em; Class<M> mclass; public EntityManager getEm() { return em; } public void setEm(EntityManager em) { this.em = em; } @Transactional(value=TxType.NOT_SUPPORTED) public M find(K id){ return em.find(mclass, id); } @Transactional(value=TxType.REQUIRED) public void insert(M inst){ this.em.persist(inst); } @SuppressWarnings("unchecked")

How frequently should I create an EntityManager?

岁酱吖の 提交于 2019-11-30 07:59:00
I have an EntityManagerFactory for which I can create one (or multiple) EntityManager instances. I'm using a Servlet environment, and I've got one EntityManagerFactory wired up to the servlet (via the servlet context) which is shared for the lifetime of the servlet (and therefore, for all users). I can do one of the following: Create a single EntityManager for the lifetime of my servlet (e.g. shared between all users) Create one per user (so each user gets their own in the HttpSession) Create one per HTTP request (say, by instantiating a new one and closing it at the end of a doGet method)

JPA 2.1 Entity Graph returns duplicated results

£可爱£侵袭症+ 提交于 2019-11-30 04:37:41
问题 I started using the new entity graph feature in JPA 2.1 to specify the Lazy collections that must be loaded. Consider following classes: @Entity @NamedQueries({ @NamedQuery(name="findWithFilterAttr","select a from A a where a.filterAttribute like :filter") }) @NamedEntityGraphs({ @NamedEntityGraph(name = "graph.childs", attributeNodes = @NamedAttributeNode("childs"))}) public class A{ @Id private long id; @OneToMany(mappedBy="parent") private List<B> childs; private String filterAttribute; }

Is there a way to pass detached object to JPA persist? (detached entity passed to persist)

筅森魡賤 提交于 2019-11-30 03:57:57
I have 2 entities : Account and AccountRole . public class Account { private AccountRole accountRole; @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) public AccountRole getAccountRole() { return accountRole; } . public class AccountRole { private Collection<Account> accounts = new ArrayList<Account>(); @OneToMany(mappedBy = "accountRole", fetch = FetchType.EAGER) public Collection<Account> getAccounts() { return accounts; } Problem comes when I take the accountRole from database and try to persist my Account . At this point I just created my account and role already exists

Is it necessary to call a flush() (JPA interface) in this situation?

有些话、适合烂在心里 提交于 2019-11-30 02:48:50
问题 Because calling a flush() to get every entities persist from memory to database. So if I use call too much unnecessary flush(), it could take much time therefore not a good choice for the performance. Here is a scenario that I don't know when to call a flush()? //Order and Item have Bidirectional Relationships Order ord = New ord("my first order"); Item item = New Item("tv",10); //...process item and ord object em.persist(ord);//em is an instance of EntityManager em.flush();// No.1 flush()

How to manually start a transaction on a shared EntityManager in Spring?

帅比萌擦擦* 提交于 2019-11-30 02:02:58
I have a LocalContainerEntityManagerFactoryBean as EntityManager instance. To quickly drop a full tables' content, I want to run the following code: @Service public class DatabaseService { @Autowired private EntityManager em; @Transactional public void clear() { em.createNativeQuery("TRUNCATE TABLE MyTable").executeUpdate(); } } Result: ERROR org.springframework.integration.handler.LoggingHandler: javax.persistence.TransactionRequiredException: Executing an update/delete query at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71) at org.springframework.cglib.proxy

What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

ぐ巨炮叔叔 提交于 2019-11-29 22:52:10
Can anybody explain what is the difference between the Spring Framework's LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean ? Basically JPA specification defines two types of entity managers . They are : i) Application-Managed : Application Managed entity manager means "Entity Managers are created and managed by merely the application ( i.e. our code )" . ii) Container Managed : Container Managed entity manager means "Entity Managers are created and managed by merely the J2EE container ( i.e. our code doesn't directly manages instead entity managers are created and

How to use JUnit tests with Spring Roo? (Problems with EntityManager)

▼魔方 西西 提交于 2019-11-29 20:02:22
问题 I'm trying to write a JUnit test for a Spring Roo project. If my test requires use of the entity classes, I get the following Exception: java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?) The Spring Aspects JAR looks to be configured correctly. In particular, I have the following in the pom.xml file: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>$

Correct way to do an EntityManager query during Hibernate Validation

半城伤御伤魂 提交于 2019-11-29 19:38:48
问题 I'm a bit of a Java EE/EJB noob, but from the docs and other posts I've gathered you cannot query the database using the same entitymanager/session during entity validation. In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context.[43] A lifecycle callback method may modify the non-relationship state of the entity on which it is invoked. Translation