Skip to content

Commit

Permalink
can't inject EntityManager by constructor spring-projects/spring-fram…
Browse files Browse the repository at this point in the history
  • Loading branch information
csieflyman committed Apr 3, 2019
1 parent 4e2019b commit 9049524
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
12 changes: 12 additions & 0 deletions application/src/main/java/auth/dao/AuthLogDaoImpl.java
Expand Up @@ -3,8 +3,20 @@
import auth.model.AuthLog;
import base.dao.AbstractJPADaoImpl;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
* @author csieflyman
*/
public class AuthLogDaoImpl extends AbstractJPADaoImpl<AuthLog, Long> implements AuthLogDao {

@PersistenceContext
private EntityManager em;

@PostConstruct
public void init() {
setEntityManager(em);
}
}
11 changes: 11 additions & 0 deletions application/src/main/java/graph/DagEdgeDaoImpl.java
Expand Up @@ -6,6 +6,9 @@
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
Expand All @@ -19,6 +22,14 @@ public abstract class DagEdgeDaoImpl<DagEdgeType extends DagEdge<VertexID>, Vert

abstract protected String getDagId();

@PersistenceContext
protected EntityManager em;

@PostConstruct
public void init() {
setEntityManager(em);
}

@Override
public void addEdges(VertexID startVertexId, VertexID endVertexId) {
Preconditions.checkArgument(startVertexId != null, "Argument [startVertexId] can not be null.");
Expand Down
11 changes: 11 additions & 0 deletions application/src/main/java/graph/IntervalTreeDaoImpl.java
Expand Up @@ -6,6 +6,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -20,6 +23,14 @@ public abstract class IntervalTreeDaoImpl<NodeType extends IntervalTreeNode<Node

abstract protected String getTreeType();

@PersistenceContext
protected EntityManager em;

@PostConstruct
public void init() {
setEntityManager(em);
}

@Override
public void addChild(NodeIdType parentNodeId, NodeIdType childNodeId) {
Preconditions.checkArgument(parentNodeId != null, "parentNodeId must not be null");
Expand Down
12 changes: 12 additions & 0 deletions application/src/main/java/log/dao/ErrorLogDaoImpl.java
Expand Up @@ -4,9 +4,21 @@
import log.model.ErrorLog;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
* @author csieflyman
*/
@Repository("errorLogDao")
public class ErrorLogDaoImpl extends AbstractJPADaoImpl<ErrorLog, Long> implements ErrorLogDao {

@PersistenceContext
private EntityManager em;

@PostConstruct
public void init() {
setEntityManager(em);
}
}
11 changes: 11 additions & 0 deletions application/src/main/java/party/dao/PartyDaoImpl.java
Expand Up @@ -8,6 +8,9 @@
import org.springframework.stereotype.Repository;
import party.model.Party;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnitUtil;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -19,6 +22,14 @@
@Repository("partyDao")
public class PartyDaoImpl<T extends Party> extends AbstractJPADaoImpl<T, UUID> implements PartyDao<T> {

@PersistenceContext
private EntityManager em;

@PostConstruct
public void init() {
setEntityManager(em);
}

@Override
public void addChild(Party parent, Party child) {
Preconditions.checkArgument(parent != null, "parent must not be null");
Expand Down
8 changes: 6 additions & 2 deletions library/src/main/java/base/dao/AbstractJPADaoImpl.java
Expand Up @@ -20,15 +20,19 @@
@Slf4j
public abstract class AbstractJPADaoImpl<T extends Identifiable<ID>, ID extends Serializable> implements GenericDao<T, ID> {

@PersistenceContext
protected EntityManager em;
private EntityManager em;

protected Class<T> clazz;

public AbstractJPADaoImpl() {
clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

// can't inject EntityManager by constructor https://github.com/spring-projects/spring-framework/issues/15076
protected void setEntityManager(EntityManager em) {
this.em = em;
}

protected T newInstance() {
try {
return clazz.newInstance();
Expand Down

1 comment on commit 9049524

@weskerhluffy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is preventing you to merge this?

Please sign in to comment.