尝试通过注释case
handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
alertDialog.show();
}
});
时,我收到NullPointerException
EntityManager
路径PersistenceContext
中的我的import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
@Stateless
public class UserService {
@PersistenceContext(unitName = "jcrdb")
private EntityManager em;
...
}
:
persistence.xml
src/main/webapp/META-INF
- 班级:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="jcrdb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- <jta-data-source>jdbc/jcrdb</jta-data-source> -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<!-- this tell Hibernate to update the DDL when it starts, very useful
for development, dangerous in production -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="javax.persistence.logging.level" value="INFO" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/jcrdb" />
<property name="javax.persistence.jdbc.user" value="jcr_admin" />
<property name="javax.persistence.jdbc.password" value="q1w2e3r4" />
<!-- <property name="tomee.jpa.factory.lazy" value="true" />-->
</properties>
</persistence-unit>
</persistence>
直接获取EntityManager没有问题:
User
我试过在码头和tomEE中运行它。
在我看来,ejb-stuff并不起作用。我在这里想念的是什么(对这一切都不熟悉)?
答案 0 :(得分:2)
EntityManagerFactory 。
@PersistenceUnit
private EntityManagerFactory emf;
您可以参考this问题和答案以获取完整的详细信息。
答案 1 :(得分:0)
你在哪里拿起那个persistence.xml
档案?我知道如果你的配置是用java(而不是XML配置)
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setPersistenceXmlLocation(
"classpath:...../persistence.xml"
);
//factory.setPersistenceUnitName("whatever");
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setDataSource(this.springJpaDataSource());
return factory;
}
您还需要设置DataSource:
@Bean
public DataSource springJpaDataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:mysql://localhost/ABCXYZ");
dataSource.setUsername("tomcatUser");
dataSource.setPassword("password1234");
return dataSource;
}
此外,您需要在配置文件
上添加@EnableTransactionManagement注释如果你想摆脱persistence.xml并且需要在java中完全进行持久化配置,那么你需要在上面替换,返回LocalContainerEntityManagerFactoryBean:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.schema-generation.database.action",
"none");
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(adapter);
factory.setDataSource(this.springJpaDataSource());
factory.setPackagesToScan("com.wrox.site.entities");
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
Configuring Persistence in Spring Framework ❘ 605
factory.setJpaPropertyMap(properties);
return factory;
}
答案 2 :(得分:0)
有时发生这种情况是因为PersistenceAnnotationBeanPostProcessor未由Spring注册。 要解决此问题,您可以按如下所示手动定义它:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
默认的PersistenceAnnotationBeanPostProcessor将由<context:annotation-config/>
和<context:component-scan/>
XML标签注册。