我正在使用 DeltaSpike 存储库在 Java SE 中持久化一个实体,并且我正在使用基于文件的 H2 数据库。问题是,虽然控制台中没有错误,但数据库中没有任何内容。我使用独立客户端检查了数据库内容。我还尝试在持久性完成后立即使用存储库获取内容,但没有得到任何结果。
当我尝试使用实体管理器(纯 JPA)直接持久化它时,它会被持久化,并且当我在 GUI 客户端中打开文件时可以看到记录。
项目结构如下:
以下是 中的依赖项pom.xml
:
<dependencies>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.6.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.core</groupId>
<artifactId>deltaspike-core-api</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.modules</groupId>
<artifactId>deltaspike-jpa-module-api</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.modules</groupId>
<artifactId>deltaspike-jpa-module-impl</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.core</groupId>
<artifactId>deltaspike-core-impl</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.modules</groupId>
<artifactId>deltaspike-data-module-api</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.modules</groupId>
<artifactId>deltaspike-data-module-impl</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.cdictrl</groupId>
<artifactId>deltaspike-cdictrl-api</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.cdictrl</groupId>
<artifactId>deltaspike-cdictrl-weld</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.5.Final</version>
</dependency>
</dependencies>
该persistence.xml
文件是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="sample-unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.one.ehsan.test.deltaspike.domain.Person</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:file:./data/test-h2-db"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<!-- Hibernate properties -->
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
实体管理器的生产者类:
@ApplicationScoped
public class PersistenceProducer
{
@Inject
@PersistenceUnitName("sample-unit")
private EntityManagerFactory entityManagerFactory;
@Produces
public EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
public void closeEntityManager(@Disposes EntityManager entityManager)
{
if (entityManager.isOpen())
{
entityManager.close();
}
}
}
实体类:
@Entity
public class Person
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private String firstName;
@Column
private String lastName;
@Temporal(TemporalType.TIMESTAMP)
@Column
private Date createDate;
// ... getter/setters omitted
}
存储库类:
@Transactional
@Repository(forEntity = Person.class)
public abstract class PersonRepository extends AbstractEntityRepository<Person, Long>
{
@Inject
private EntityManager entityManager;
}
调用存储库的服务 bean:
public class PersonService
{
@Inject
private EntityManager entityManager;
@Inject
private PersonRepository personRepository;
public Person save(Person person) {
// I tried removing begin/commit and nothing changed
entityManager.getTransaction().begin();
Person result = personRepository.save(person);
entityManager.getTransaction().commit();
return result;
}
}
应用程序的主要类:
public class Main
{
private CdiContainer cdiContainer;
public static void main(String[] args)
{
Main main = new Main();
main.run();
}
public void run() {
cdiContainer = CdiContainerLoader.getCdiContainer();
cdiContainer.boot();
persistPerson();
cdiContainer.shutdown();
}
private void persistPerson()
{
PersonService personService = (PersonService) getBean(PersonService.class);
Person person = new Person();
person.setFirstName("Foo");
person.setLastName("Bar");
person.setCreateDate(new Date());
personService.save(person);
}
private Object getBean(Class type)
{
BeanManager beanManager = cdiContainer.getBeanManager();
Set<Bean<?>> personServiceBean = beanManager.getBeans(type);
Bean<?> bean = beanManager.resolve(personServiceBean);
CreationalContext<?> context = beanManager.createCreationalContext(bean);
return beanManager.getReference(bean, type, context);
}
}
有人知道(可能)DeltaSpike 配置中可能遗漏了什么吗?还是这种配置中的预期行为?