我是JPA/OpenJPA的新手,我注意到如果我EntityManager.clear()在持久化实体之后不调用,我会得到一个OutOfMemoryError(我继续在循环中添加新实体).我不确定这是否是预期的行为,或者只是OpenJPA 1.2.1故障.
那么,我是否需要自己明确分离实体?如果我不是,那么这是一个很好的做法?
我正在使用Spring + Hibernate + JPA,我遇到的情况是我无法让我的实体持久存储到数据库中.我已经设置了一个使用@Transactional注释的服务类.它使用包含注入的EntityManager的DAO.当我在服务对象上调用该函数时,我看到DAO正在执行的读取的一堆选择,但是由于我的DAO发出的合并和删除没有更新/删除.当然我的设置有问题,但我看不到它.
persistence.xml中
<persistence 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"
version="1.0">
<persistence-unit name="pu">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
<property name="hibernate.showsql" value="true" />
<property name="hibernate.cache.use_second_level_cache"
value="false" />
</properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
config.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/testdb" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="pu" />
<property …Run Code Online (Sandbox Code Playgroud) 在我的应用程序管理事务的情况下,我要选择:
EntityManager并调用clear().分享EntityManager使用a ThreadLocal.EntityManager为每个事务创建一个新的.我对JPA没有多少经验.我的问题是哪一个在性能方面更好?
我已经设置了一个包含多个实体管理器的新项目,当我尝试加载数据夹具时,我得到一个MappingException,因为控制台试图为所有内容加载Fixtures,而不是我指定的实体管理器.
这是我的config.yml中的学说部分
doctrine:
dbal:
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
symblog:
driver: %database_driver_blog%
host: %database_host_blog%
port: %database_port_blog%
dbname: %database_name_blog%
user: %database_user_blog%
password: %database_password_blog%
charset: %database_charset_blog%
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
connection: default
mappings:
IncompassAuthBundle: ~
IncompassUserBundle: ~
IncompassApiBundle: ~
IncompassSurgeryBundle: ~
IncompassVendorBundle: ~
IncompassHospitalBundle: ~
dql:
datetime_functions:
date: Mapado\MysqlDoctrineFunctions\DQL\MysqlDate
symblog:
connection: symblog
mappings:
IncompassBlogBundle: ~
dql:
datetime_functions:
date: Mapado\MysqlDoctrineFunctions\DQL\MysqlDate
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我为symblog教程设置了一个单独的连接和实体管理器.
当我尝试
php app/console doctrine:fixtures:load --em=default
Run Code Online (Sandbox Code Playgroud)
我明白了
Careful, database will be …Run Code Online (Sandbox Code Playgroud) 对该主题的各种部分描述使我怀疑BMT与应用程序管理的EntityManager(以及UserTransaction的使用)紧密相关,并且CMT与容器管理的EntityManager紧密相关.
任何人都可以(或指向我)清楚地解释BMT/CMT与应用程序/容器管理的EntityManager的确切关系吗?
事务划分类型和EntityManager管理类型之间允许哪些组合?
此外,UserTransaction和BMT/CMT与应用程序/容器管理的EntityManager之间的关系是什么?
编辑:根据http://www.byteslounge.com/tutorials/container-vs-application-managed-entitymanager,我可以将CMT/BMT与应用程序/容器管理的EntityManagers结合起来.这仍然让我有关于UserTransaction和BMT之间关系的问题.一个人是否意味着另一个?
编辑:与上面发布的链接相反,http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html#bnbra声称" 应用程序管理的实体管理器不会自动传播JTA事务上下文.这些应用程序需要手动访问JTA事务管理器并在执行实体操作时添加事务划分信息.javax.transaction.UserTransaction接口定义了开始,提交和回滚事务的方法.通过创建实例来注入UserTransaction实例变量用@Resource注释".对我来说,这听起来像"应用程序管理的实体管理器需要bean管理的事务划分".谁是对的?Oracle还是Byteslounge?
jpa transactions entitymanager java-ee bean-managed-transactions
我正在尝试使用jpa的entityManager的createNativeQuery方法映射非实体pojo.通过使用这样的东西
@SqlResultSetMapping(name="ResultMapping",
classes={
@ConstructorResult(
targetClass=Employee.class,
columns={
@ColumnResult(name="empID", type=Long.class),
@ColumnResult(name="empName", type=String.class),
}
)
}
)
public class Loader{
private EntityManager em;
public void load(){
Query query = em.createNativeQuery("select empID, empName from employee", "ResultMapping");
List<Employee> = query.getResultList();
}
}
public class Employee{
private long empID;
private String empName;
public Employee(long empid, String empname)
{
this.empID = empid;
this.empName = empname;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到unknown SqlResultSetMapping ResultMapping错误我应该把SqlResultSetMapping放在哪里,以便entityManager能够识别它?
我在过去构建了一些JPA的东西,它们使用了javax.persistence.EntityManager每个DAO实例的一个实例; 这是大多数示例的设置方式.
public class BaseDaoThatEveryDaoExtends {
@PersistenceContext
private EntityManager entityManager;
}
Run Code Online (Sandbox Code Playgroud)
我只是偶然发现使用注释注入的静态 代码,架构师告诉我这不会引起任何问题,即使在具有JTA和XA数据源的集群应用程序中也没有任何问题:javax.peristence.EntityMangerPersistenceContext
public class BaseDaoThatEveryDaoExtends {
@PersistenceContext
private static EntityManager entityManager;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这是一个反模式,因为它EntityManager保存一些状态信息并使其静态使整个状态应用程序广泛.这也使得课程很难测试.
这样做有其他缺点还是这是使用标准的方法EntityManager?
我没有使用Spring,所以我在一个类中创建一个EntityManager实例.
我使用Hibernate-Eclipse逆向工程来自动生成类.这些类都有一个EntityManager实例.
我不是100%确定Hibernate如何与EntityManager一起工作,所以我想知道这个类(EntityManager)的这么多实例是否可以,例如,交易会出现问题吗?
我是否应该创建一个单独的类来为所有其他类分发EntityManager的静态实例?或者没关系?
编辑:我看到有一些名为@PersistenceContext的东西,它似乎没有将我的persistence.xml作为bean加载到实例变量中,这个功能是否需要spring?(我得到空指针异常,因为它从未被注入)
从我尝试使用@persistencecontext的代码片段
@PersistenceContext(unitName = "manager1")
private EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)
我的persistence.xml
<persistence 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"
version="2.0">
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="mypassword"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/ptbrowserdb"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud) 在构造bean之后,我想使用EntityManager从数据库中检索数据.在构造函数中是不可能的,因为在调用构造函数之后注入EntityManager.所以我尝试在用@PostConstruct注释的方法中做到这一点.根据API,在完成所有注入后调用PostConstruct方法.执行查询有效,但它总是返回一个空列表.如果我在其他方法中使用相同的查询,则返回正确的结果.有谁知道,为什么它在PostConstruct方法中不起作用?
@Stateful(mappedName = "price")
@Singleton
@Startup
public class PriceManagementBean implements PriceManagement {
@PersistenceContext
private EntityManager em;
private List<PriceStep> priceSteps = Collections.synchronizedList(new ArrayList<PriceStep>());
public PriceManagementBean(){
}
@PostConstruct
public void init(){
javax.persistence.Query query = em.createQuery("SELECT ps FROM PriceStep ps");
List<PriceStep> res = query.getResultList();
.....
}
}
Run Code Online (Sandbox Code Playgroud) 在我的Web应用程序中,我在Apache Tomcat(TomEE)/7.0.37服务器上使用OpenJPA.我使用Netbeans自动生成类("实体类来自数据库......"和"会话Bean来自实体类......").在SessionBean(例如UserFacade)我想获得EntityManager:
@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext(unitName = "CollDocPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我通过上面的方式得到它时,我得到了null.当我通过:
@Override
protected EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU");
EntityManager ecm = emf.createEntityManager();
return ecm;
}
Run Code Online (Sandbox Code Playgroud)
ecm不是null,没关系
我的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="CollDocPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.entity.StudentAddSolution</class>
<class>model.entity.Lecturer</class>
<class>model.entity.Solution</class>
<class>model.entity.Student</class>
<class>model.entity.Course</class>
<class>model.entity.File</class>
<class>model.entity.CourseHasLecturer</class>
<class>model.entity.Mail</class>
<class>model.entity.StudentAtCourse</class>
<class>model.entity.Roles</class>
<class>model.entity.Task</class>
<class>model.entity.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/myBase?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="pass,"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property …Run Code Online (Sandbox Code Playgroud)