将 spring data jpa 与 ninja java 一起使用
Posted
技术标签:
【中文标题】将 spring data jpa 与 ninja java 一起使用【英文标题】:use spring data jpa with ninja java 【发布时间】:2017-08-26 21:49:42 【问题描述】:ninja 是一个很棒的框架,但是来自 spring 背景,我需要将 spring data jpa 与 ninja 一起使用。
我想自动装配一个 JpaRepository 并在 ninja 中使用它。虽然 ninja 使用 Guice Inject。我不断收到没有为我的班级找到的实现。
存储库:
public interface PortalUserRepository extends JpaRepository<PortalUser, Long>
PortalUser getPortalUserByUsername(String username);
PortalUser getPortalUserByEmail(String email);
注射
public class SetupDaoV2
@Inject
PortalUserRepository portalUserRepository;
public void setup()
try
List<PortalUser> portalUsers = portalUserRepository.findAll();
if (portalUsers.size() == 0)
// Create a new user and save it
PortalUser portalUser = new PortalUser("lsadjfl", "lsdlfs", "kkk lll",
"llskfls@gmail.com", "lsdlfss@",
"lsdfls@gmail.com",
new Timestamp(System.currentTimeMillis()), Boolean.TRUE,
Boolean.TRUE, GenericStatusConstant.ACTIVE, Boolean.TRUE
);
portalUserRepository.save(portalUser);
catch (Exception ex)
ex.printStackTrace();
错误
com.google.inject.CreationException:无法创建注入器,请参阅 以下错误:
1) 没有绑定 com.bw.dd.dsl.repository.PortalUserRepository 的实现。
【问题讨论】:
Spring Data JPA 仅适用于 Spring 或 CDI。如果您想将它与 Google Guice 一起使用,您必须自己完成所有的连接和生成工作。 @dunni。必须有一种方法可以将 spring data jpa 移植到 ninja 中,而无需编写整个实现。 好吧,您可以查看 CDI 扩展并尝试将其移植到 Google Guice。但除此之外,没有更简单的可能性。 【参考方案1】:好吧,我终于不得不实施整个事情。遗憾的是,我无法将 spring data jpa 中的精彩功能移植到 ninja 中。我可以使用弹簧,但用于该项目的办公室政策。
如果有人找到方法,请大家好。
public abstract class JpaRepository<E, K>
public abstract void create(E entity);
public abstract void edit(E entity) throws Exception;
public abstract void remove(K key) throws IllegalOrphanException, NonexistentEntityException;
public abstract List<E> findAll();
public abstract E findOne(K key);
public abstract int count();
public class PortalUserRepository extends JpaRepository<PortalUser, Long>
private EntityManagerFactory emf = null;
public PortalUserRepository()
public PortalUserRepository(EntityManagerFactory emf)
this.emf = emf;
public EntityManager getEntityManager()
return emf.createEntityManager();
public void create(PortalUser entity)
if (entity.getUserTokens() == null)
entity.setUserTokens(new ArrayList<UserToken>());
EntityManager em = null;
try
em = getEntityManager();
em.getTransaction().begin();
Collection<UserToken> attachedUserTokens = new ArrayList<UserToken>();
for (UserToken userTokensUserTokenToAttach : entity.getUserTokens())
userTokensUserTokenToAttach = em.getReference(userTokensUserTokenToAttach.getClass(), userTokensUserTokenToAttach.getId());
attachedUserTokens.add(userTokensUserTokenToAttach);
entity.setUserTokens(attachedUserTokens);
em.persist(entity);
for (UserToken userTokensUserToken : entity.getUserTokens())
PortalUser oldPortalUserOfUserTokensUserToken = userTokensUserToken.getPortalUser();
userTokensUserToken.setPortalUser(entity);
userTokensUserToken = em.merge(userTokensUserToken);
if (oldPortalUserOfUserTokensUserToken != null)
oldPortalUserOfUserTokensUserToken.getUserTokens().remove(userTokensUserToken);
oldPortalUserOfUserTokensUserToken = em.merge(oldPortalUserOfUserTokensUserToken);
em.getTransaction().commit();
finally
if (em != null)
em.close();
public void edit(PortalUser entity) throws Exception
EntityManager em = null;
try
em = getEntityManager();
em.getTransaction().begin();
PortalUser persistentPortalUser = em.find(PortalUser.class, entity.getId());
Collection<UserToken> userTokensOld = persistentPortalUser.getUserTokens();
Collection<UserToken> userTokensNew = entity.getUserTokens();
List<String> illegalOrphanMessages = null;
for (UserToken userTokensOldUserToken : userTokensOld)
if (!userTokensNew.contains(userTokensOldUserToken))
if (illegalOrphanMessages == null)
illegalOrphanMessages = new ArrayList<String>();
illegalOrphanMessages.add("You must retain UserToken " + userTokensOldUserToken + " since its entity field is not nullable.");
if (illegalOrphanMessages != null)
throw new IllegalOrphanException(illegalOrphanMessages);
Collection<UserToken> attachedUserTokensNew = new ArrayList<UserToken>();
for (UserToken userTokensNewUserTokenToAttach : userTokensNew)
userTokensNewUserTokenToAttach = em.getReference(userTokensNewUserTokenToAttach.getClass(), userTokensNewUserTokenToAttach.getId());
attachedUserTokensNew.add(userTokensNewUserTokenToAttach);
userTokensNew = attachedUserTokensNew;
entity.setUserTokens(userTokensNew);
entity = em.merge(entity);
for (UserToken userTokensNewUserToken : userTokensNew)
if (!userTokensOld.contains(userTokensNewUserToken))
PortalUser oldPortalUserOfUserTokensNewUserToken = userTokensNewUserToken.getPortalUser();
userTokensNewUserToken.setPortalUser(entity);
userTokensNewUserToken = em.merge(userTokensNewUserToken);
if (oldPortalUserOfUserTokensNewUserToken != null && !oldPortalUserOfUserTokensNewUserToken.equals(entity))
oldPortalUserOfUserTokensNewUserToken.getUserTokens().remove(userTokensNewUserToken);
oldPortalUserOfUserTokensNewUserToken = em.merge(oldPortalUserOfUserTokensNewUserToken);
em.getTransaction().commit();
catch (Exception ex)
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0)
Long id = entity.getId();
if (findOne(id) == null)
throw new NonexistentEntityException("The entity with id " + id + " no longer exists.");
throw ex;
finally
if (em != null)
em.close();
public void remove(Long key) throws IllegalOrphanException, NonexistentEntityException
EntityManager em = null;
try
em = getEntityManager();
em.getTransaction().begin();
PortalUser portalUser;
try
portalUser = em.getReference(PortalUser.class, key);
portalUser.getId();
catch (EntityNotFoundException enfe)
throw new NonexistentEntityException("The portalUser with id " + key + " no longer exists.", enfe);
List<String> illegalOrphanMessages = null;
Collection<UserToken> userTokensOrphanCheck = portalUser.getUserTokens();
for (UserToken userTokensOrphanCheckUserToken : userTokensOrphanCheck)
if (illegalOrphanMessages == null)
illegalOrphanMessages = new ArrayList<String>();
illegalOrphanMessages.add("This PortalUser (" + portalUser + ") cannot be destroyed since the UserToken " + userTokensOrphanCheckUserToken + " in its userTokens field has a non-nullable portalUser field.");
if (illegalOrphanMessages != null)
throw new IllegalOrphanException(illegalOrphanMessages);
em.remove(portalUser);
em.getTransaction().commit();
finally
if (em != null)
em.close();
public List<PortalUser> findAll()
EntityManager em = getEntityManager();
try
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(PortalUser.class));
Query q = em.createQuery(cq);
return q.getResultList();
finally
em.close();
public PortalUser findOne(Long key)
EntityManager em = getEntityManager();
try
return em.find(PortalUser.class, key);
finally
em.close();
public int count()
EntityManager em = getEntityManager();
try
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<PortalUser> rt = cq.from(PortalUser.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
finally
em.close();
public PortalUser findPortalUserByUsername(String username)
EntityManager em = getEntityManager();
try
Query q = em.createQuery("select s from PortalUser s where s.username = '" + username + "'");
return (PortalUser)q.getSingleResult();
finally
em.close();
【讨论】:
【参考方案2】:您需要将PortalUserRepository
类标记为@Repository
,如下所示:
@Repository
public interface PortalUserRepository extends JpaRepository<PortalUser, Long>
//your methods
另外,确保您拥有 @EnableJPARepositories
,以便 Spring 容器可以扫描您的存储库接口并为它们提供实现,然后将它们注入您的 SetupDaoV2
bean。
【讨论】:
OP 不使用 Spring,他使用 Guice。除此之外,@Repository
注解在 Spring Data 存储库中是不必要的(如果您阅读注解的 Javadoc,应该很清楚)。
你能指出你所指的API中的文字吗?我找不到..我知道我们可以使用任何刻板印象让 Spring 容器进行扫描
是的,这适用于用作 bean 的类,而不是 Spring Data 的接口。 Javadoc 说“表明一个带注释的类是一个”存储库“”。这些接口已经通过扩展 Spring 数据库接口(如 CrudRepository 或 JpaRepository)进行了标记。这个和@EnableJpaRepositories
就够了。
您还可以查看 Spring Data 的示例 (github.com/spring-projects/spring-data-examples),您会发现没有一个接口上有 @Component
或 @Repository
。
最后一件事,您能指出您所指的 Java 文档吗?以上是关于将 spring data jpa 与 ninja java 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
将 Spring Data JPA 与 GCP Spanner 集成
将 Hibernate Sessions 功能与 Spring Data JPA 一起使用
如何将多个日期之间的搜索与 Spring Data JPA 的 CrudRepository 结合起来?
spring-data-jpa 和 spring-boot-starter-data-jpa 的区别
将多个数据库与 Spring Data JPA 一起使用时,不会发生名为“ConfigurationClassPostProcessor.importRegistry”的 bean 异常