在JPA中,一个没有持久标识但被管理的实体是如何描述的?
引用第76页 实体实例可以被描述为新的、受管理的、, 分离的,分离的 •新实体实例没有持久标识,并且还没有 与持久性上下文关联 •托管实体实例是具有持久标识的实例 当前与持久性上下文关联的 •分离实体实例是具有持久标识的实例 与持久性上下文不关联(或不再关联)的 •删除的实体实例是具有持久标识的实例, 与持久性上下文关联,将从 事务提交时的数据库 假设我有以下实体类在JPA中,一个没有持久标识但被管理的实体是如何描述的?,jpa,Jpa,引用第76页 实体实例可以被描述为新的、受管理的、, 分离的,分离的 •新实体实例没有持久标识,并且还没有 与持久性上下文关联 •托管实体实例是具有持久标识的实例 当前与持久性上下文关联的 •分离实体实例是具有持久标识的实例 与持久性上下文不关联(或不再关联)的 •删除的实体实例是具有持久标识的实例, 与持久性上下文关联,将从 事务提交时的数据库 假设我有以下实体类 @Entity public class Employee { private long id; private
@Entity
public class Employee {
private long id;
private String firstname;
private String lastname;
private String address;
private String city;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
return id;
}
// Rest of the getters, setters..
}
如果实体实例没有持久标识,但由EntityManager管理,那么它属于哪个特征
有关具体示例,请参见以下方法中的注释:
public void insert(final Employee employee) {
final EntityManager entityManager = entityManagerService.entityManager();
entityManager.persist(employee);
// The instance 'employee' is managed by the EntityManager but does not have a
// persistent identity at this point?
// In which characterization does an Entity instance fall in, at this point of execution?
System.out.println(employee.getId()); // Will print 0
entityManager.getTransaction().begin();
entityManager.getTransaction().commit();
System.out.println(employee.getId()); // Will print 1 with an empty DB..
entityManager.close();
}
我的理解是不是错误的,在给定的点上,实体是被管理的,但没有持久的标识
我认为这与此无关,但这是我的persistence.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="2.0">
<persistence-unit name="jpaExamples" transaction-type="RESOURCE_LOCAL">
<class>biz.tugay.jpaExamples.model.Employee</class>
<properties>
<!-- Properties here.. -->
</properties>
</persistence-unit>
</persistence>
在事务中持久化对象。然后它被管理!您的代码在事务之外被持久化,因此您可能会遇到异常(取决于JPA提供者)@NeilStockton上述方法工作正常,我不确定您的语句是否回答了我的问题。不过还是谢谢你。正如我说的。。。这取决于您的JPA提供商(无论是哪个)在那里做什么。也许是“自动提交”?在这种情况下,它将被分离,具体取决于所使用的持久性上下文。同样,您没有定义足够的信息,JPA提供商的日志会解释这一方法more@NeilStockton我在上面的例子中使用Hibernate,没有任何异常。您是否建议如果启用“自动提交”,实体将立即获得持久性标识?如果自动提交关闭怎么办?@NeilStockton我在insert方法中添加了一些额外的语句。
package biz.tugay.jpaExamples.service;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class EntityManagerServiceByPersistenceUnitNameImpl implements EntityManagerService {
private static EntityManagerFactory entityManagerFactory;
private EntityManagerServiceByPersistenceUnitNameImpl() {
// Use the static method serviceForPersistenceUnitName to get an instance of this class!
}
public static EntityManagerService serviceForPersistenceUnitName(final String persistenceUnitName) {
if (entityManagerFactory == null || entityManagerFactory.isOpen()) {
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
}
return new EntityManagerServiceByPersistenceUnitNameImpl();
}
@Override
public EntityManager entityManager() {
final EntityManager entityManager = entityManagerFactory.createEntityManager();
return entityManager;
}
@Override
public void closeEntityManagerFactory() {
entityManagerFactory.close();
}
}