1

我有一个方法需要在我的实体类 company.java 中调用。但是当我运行我的应用程序时,它抛出空指针异常并没有在实体类中找到那个 DAO 对象..

我怎样才能在实体类中获取该对象请帮助

这是我的实体类..

package com.salebuild.model;

/**
 * Define a company.
 *
 * @author mseritan
 */
@Entity
@Table(name = "company", uniqueConstraints = {@UniqueConstraint(columnNames = "name")})
@XmlRootElement
@XmlSeeAlso(value = ArrayList.class)
public class Company implements PublishableIF, Serializable, PersistableIF, HistoryIF, AddressableIF {
     private static final Logger log = LoggerFactory.getLogger( Company.class );
     @Autowired
        private CompanyDAO companyDAO;

    // CONSTANTS -----------------------------------------------------------------------------------

    private static final long serialVersionUID = 1L;

    // ATTRIBUTES ----------------------------------------------------------------------------------

    private Long id;
    @NotBlank
    private String name;
    private String formerName;
    private CorporateTitle topLevelExec;
    private List<CompanySite> sites;
    private List<CompanyAlias> aliases;
    @NotNull
    private Industry industry;
    private Company parentCompany;
    private String emailTopology;
    @NotNull
    private Double revenue;

    @NotNull
    private Long numberEmployees;
    private CustomerType.Type customerType;
    private Boolean recruiter = false;
    private int publishState;
    private CompanyStatus status;
    private Boolean excludeCompany = false;
    private CompanyType companyType;
    private String salesifyCompanyId;
    private CompanySiteType companySiteType;
    private String websiteUrl;
    private String sourceVendor;
    private String notes;
    private List<CompanySpecializedRanking> specializedList = new ArrayList<CompanySpecializedRanking>();
    @NotNull
    private NAICSCode naicsCode;
    @NotNull
    private SICCode sicCode;
    private Long version;
    private List<Technology> technologies = new ArrayList<Technology>();
    private List<CompanyContact> contacts;
    private String phoneNumber;
    private String faxNumber;
    private String email;

    private User userCreated;
    private Date dateCreated;

    private User userLastModified;
    private Date dateLastModified;

    private User userLastResearcher;
    private Date dateLastResearcher;
    @NotBlank
    private String street1;
    private String street2;
    private String street3;
    private String city;
    private String zipCode;
    private State state;
    private Country country;
    private String specRankingListName;
    private Integer specRankingRank;
    private Integer specRankingYear;
    private String modifiedCompanyName;

    private String formattedRevenue;
    private String formattedEmployeeSize;
    private List<JobPostingRaw> unconfirmedTechnologies;
    // ACESSORS ------------------------------------------------------------------------------------
   //getter setter for other fields //
        this.specRankingYear = specRankingYear;
    }


    /**
     * @param modifiedCompanyName  
     *
     */
    @Column(name="modifiedCompanyName")
    public String getModifiedCompanyName() {
        return modifiedCompanyName;
    }

    public void setModifiedCompanyName(String modifiedCompanyName) {
        if(modifiedCompanyName==null)
            this.modifiedCompanyName=modifiedCompanyName;
        else{
         this.modifiedCompanyName =companyDAO.updateCompanyName(modifiedCompanyName);
        }
    }

    @Transient
    public List<JobPostingRaw> getUnconfirmedTechnologies() {
        return unconfirmedTechnologies;
    }

    public void setUnconfirmedTechnologies(
            List<JobPostingRaw> unconfirmedTechnologies) {
        this.unconfirmedTechnologies = unconfirmedTechnologies;
    }



}

我的 DAO 课是这样的——

package com.salebuild.dao;

import com.salebuild.model.Company;
import com.salebuild.model.search.EntitySearchCriteria;
import com.salebuild.model.search.SortedResultsPage;

import java.util.Collection;
import java.util.List;
import java.util.Set;

public interface CompanyDAO extends CrudDAO<Company> {
    Company findByNameOrAlias(String name);

    List<Company> findBySearchTerm(String searchTerm, Integer start, Integer count);

   // SortedResultsPage<Company> findPaged(EntitySearchCriteria criteria);

    List<Long> findIds(EntitySearchCriteria criteria);

    List<Company> find(Collection<Long> ids);

    /**
     * For just finding the company name and not looking for alias names.
     *
     * @param name
     * @return
     */
    public Company findByName( String name );
    public Company findByModifiedName(String name,Company... c);

    public int companyCountSinceLastLogin(Long id);

    Set<Long> findDistinctIds(EntitySearchCriteria criteria);

    public Integer getCompanyCountByRegion(Long regionId,List techCatIds);

    List<Company> findAllCompanies(Company instance);

    public List<Company> findAllModifiedCompanies(Company instance);

    public String updateCompanyName(String name);
}
4

2 回答 2

2

最简单的选择是为构建实体实现工厂。然后你可以使用 AutowireCapableBeanFactory 来自动装配依赖:

public abstract class GenericFactory<T> {

  @Autowired
  private AutowireCapableBeanFactory autowireBeanFactory;

  public T createBean() {
    // creation logic
    autowireBeanFactory.autowireBean(createdBean);
    return createdBean;
  }
}

当然,您可以传递对象(创建或检索)并自动装配它。

另一种选择是使用 @Configurable - 它会自动注入依赖项。http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable

您可以在我的帖子中找到更多详细信息:http ://www.kubrynski.com/2013/09/injecting-spring-dependencies-into-non.html

于 2014-01-09T22:24:03.143 回答
0

JPA 实体是 POJO,即简单的 Java bean,它们没有依赖关系,并且具有不包含复杂逻辑的 getter 和 setter。

最好创建一个服务来负责保存和更新在整个代码中使用的实体。然后,此服务可以负责使用可以自动装配的依赖项执行您希望放入 setter 的逻辑。

您遇到的问题是您的 Spring 不负责创建您的实体。您可以使用它来实例化它,new也可以从 JPA 实现中获取它。无论哪种方式,Spring 都没有机会自动装配声明的依赖项。


顺便说一句,自动装配私有变量并不是一个好习惯。有关更全面的讨论,请参阅此博客文章

于 2014-01-09T22:34:09.870 回答