我有一个方法,我需要在我的实体类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);
}
答案 0 :(得分: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
答案 1 :(得分:0)
JPA实体应该是POJO,即简单的Java bean,它们没有依赖关系,并且具有不包含复杂逻辑的getter和setter。
最好创建一个负责保存和更新整个代码中使用的实体的服务。然后,该服务可以负责使用可以自动装配的依赖项执行您希望放入setter的逻辑。
您遇到的问题是Spring不负责创建您的实体。您可以使用new
对其进行实例化,也可以从JPA实现中获取它。无论哪种方式,Spring都没有机会自动声明已声明的依赖项。
除此之外,自动变量私有变量并不是一个好习惯。有关更全面的讨论,请参阅此blog post。