Java 使@实体自身持久化
给出一个典型的JPA示例,我们有如下代码:Java 使@实体自身持久化,java,jpa,Java,Jpa,给出一个典型的JPA示例,我们有如下代码: @Entity public class Company { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; //...etc... } 以及: (对于本例,让我们忽略@Stateless属性) 在阅读其他网站和本问答时,我为什么不能说: @Entity public class Company { @Persistenc
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//...etc...
}
以及:
(对于本例,让我们忽略@Stateless
属性)
在阅读其他网站和本问答时,我为什么不能说:
@Entity
public class Company {
@PersistenceContext(unitName = "custdb")
private EntityManager em;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public void create() {
em.persist(this);
}
//...etc...
}
这样做会让我自己陷入一个受伤的世界吗?一个可能的原因是,与在每个类中重复CRUD(创建、读取、更新、删除)操作不同,您可以创建更通用的解决方案,允许您处理多个类的实例 看看这个。接受的答案给出了一个通用DAO的示例,该DAO使用其操作的对象类型参数化。这是一个非常常用的模式。通过使用JPA(Hibernate)和Spring测试,我能够证明这是可能的。 假设这是你相当开明的实体: models.so.haum.IntropectiveEntity.java
@Entity
public class IntrospectiveEntity {
@Transient
private EntityManager em;
protected IntrospectiveEntity() { }
public IntrospectiveEntity(final EntityManager em) {
this.em = em;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public void create() {
em.persist(this);
}
public List<IntrospectiveEntity> all() {
TypedQuery<IntrospectiveEntity> query = em.createQuery("SELECT ie FROM IntrospectiveEntity ie", IntrospectiveEntity.class);
return query.getResultList();
}
}
它成功地通过了。坚持并发现自己。
所以,如果你真的想这样做,这是可能的。显然,没有被注入的
EntityManager
可能不是您想要的,但您已经拥有了它 如果你要走这条路,为什么不直接使用SpringDataJPA呢?为您提供了上述模式的所有好处,以及更多好处—请看一看SpringRoo,尤其是@RooActiveRecord
。
@Entity
public class IntrospectiveEntity {
@Transient
private EntityManager em;
protected IntrospectiveEntity() { }
public IntrospectiveEntity(final EntityManager em) {
this.em = em;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public void create() {
em.persist(this);
}
public List<IntrospectiveEntity> all() {
TypedQuery<IntrospectiveEntity> query = em.createQuery("SELECT ie FROM IntrospectiveEntity ie", IntrospectiveEntity.class);
return query.getResultList();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class IntrospectiveEntityTest {
@PersistenceContext
private EntityManager em;
@Test
@Transactional
public void namaste() {
IntrospectiveEntity introspectiveEntity = new IntrospectiveEntity(em);
introspectiveEntity.create();
assertThat(introspectiveEntity.all().size(), IsEqual.equalTo(1));
}
@Configuration
@ComponentScan(basePackages = "so")
static class IntrospectiveEntityConfiguration {
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManager(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setShowSql(true);
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan("so.models.haum");
return entityManagerFactory;
}
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase dataSource = builder.setType(EmbeddedDatabaseType.H2).build();
return dataSource;
}
}
}