我想在我的数据库中保留一个对象,我正在使用来自Guice的@Trasactional和@Inject来保存数据.... hibernate配置在ressources / meta-inf文件夹中的文件persistence.xml中声明....
import ca.model.jpa.user;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import javax.persistence.EntityManager;
import javax.ws.rs.*;
@Path("/test")
public class TestService {
@Inject
private EntityManager entityManager;
@GET
@Transactional
public String persistUser() {
// User
User user = new User();
user.setPseudoname("Anonymous");
user.setAge(20);
entityManager.persist(user);
return null;
}
当我调试代码时,entityManager始终为null ...在我的理解中,注入旨在启动entityManager。但它没有这样做!
如何更正我的代码?谢谢
这里是我的文件persistence.xml的内容:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!--PostgreSQL-->
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="postgres"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.ejb.metamodel.generation" value="disabled"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:0)
你能分享你的persistence.xml吗?因为在此文件中,您可以声明实体管理器的事务类型。请确保您已正确列出。
这里还有一个不错的链接:http://piotrnowicki.com/2012/11/types-of-entitymanagers-application-managed-entitymanager/
答案 1 :(得分:0)
您的EntityManager未注入。
您是否实施了持久性模块?
public class ExampleHibernateModule extends PrivateModule {
@Override
public void configure() {
install(new JpaPersistModule("example-persistence-unit").properties(jpaProperties()));
bind(ExampleAO.class).to(ExampleImpl.class);
expose(ExampleDAO.class);
Key<PersistFilter> key = Key.get(PersistFilter.class, ExamplePersistenceUnit.class);
bind(key).to(PersistFilter.class);
expose(key);
}
Properties jpaProperties() {
Properties appProperties = PropertiesUtils.getApplicationProperties();
Properties properties = new Properties();
properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
properties.put("hibernate.connection.url", appProperties.get("hibernate.connection.url"));
properties.put("hibernate.connection.username", appProperties.get("hibernate.connection.username"));
properties.put("hibernate.connection.password", appProperties.get("hibernate.connection.password"));
properties.put("hibernate.connection.pool_size", "1");
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.hbm2ddl.auto", appProperties.get("hibernate.hbm2ddl.auto"));
properties.put("hibernate.show_sql", appProperties.get("hibernate.show_sql"));
properties.put("hibernate.format_sql", appProperties.get("hibernate.format_sql"));
properties.put("hibernate.cache.use.query_cache", "true");
properties.put("hibernate.cache.use_second_level_cache", "true");
return properties;
}
}