我无法弄清楚DAO类无法管理实体对象的原因。
我有以下PersistenceConfig文件:
@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:persistence"})
@EnableJpaRepositories("com.wx.rm")
public class PersistenceConfig {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[]{"com.wx.rm"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
setProperty("hibernate.globally_quoted_identifiers",
env.getProperty("hibernate.globally_quoted_identifiers"));
}
};
}
}
接口IFooAdvertDao:
@Repository("fooAdvertDao")
public interface IFooAdvertDao extends GenericDao<FooAdvert, String> {
}
接口IBarAdvertDao:
@Repository("barAdvertDao")
public interface IBarAdvertDao extends GenericDao<BarAdvert, Integer> {
}
基础实体BaseAdvert类:
@MappedSuperclass
public abstract class BaseAdvert implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name = "link")
private String link;
@Column(name = "rooms")
private BigDecimal rooms;
public BaseAdvert() {
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public BigDecimal getRooms() {
return rooms;
}
public void setRooms(BigDecimal rooms) {
this.rooms = rooms;
}
}
实体FooAdvert类:
@Entity
@Table(name = "foo_adverts")
@DynamicUpdate
public class FooAdvert extends BaseAdvert implements Serializable {
@Id
private String id;
@Column(name = "type")
private Integer type;
public FooAdvert() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
实体BarAdvert类:
@Entity
@Table(name = "bar_adverts")
@DynamicUpdate
public class BarAdvert extends BaseAdvert implements Serializable {
@Id
private Integer id;
@Column(name = "status")
private Integer status;
public BarAdvert() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
那是我的GenericDao课程:
@Repository
public interface GenericDao<T extends Serializable, ID extends Serializable> extends JpaRepository<T, ID> {
}
通用服务类:
public interface GenericService <T extends Serializable, ID extends Serializable> {
T findOne(final ID id);
Page<T> findAll(final Pageable pageable);
long count();
}
通用服务实现类:
@Service
@Transactional
public class GenericServiceImpl <T extends Serializable, ID extends Serializable> implements GenericService<T, ID> {
@Autowired
private GenericDao<T, ID> genericDao;
public GenericServiceImpl() {
}
public GenericServiceImpl(GenericDao<T, ID> genericDao) {
this.genericDao = genericDao;
}
@Override
@Transactional(readOnly = true)
public T findOne(ID id) {
return genericDao.findOne(id);
}
@Override
@Transactional(readOnly = true)
public Page<T> findAll(Pageable pageable) {
return genericDao.findAll(pageable);
}
@Override
@Transactional(readOnly = true)
public long count() {
return genericDao.count();
}
}
FooAdvertService:
@Service
public class FooAdvertService extends GenericServiceImpl<FooAdvert, String> {
@Autowired
private IFooAdvertDao fooAdvertDao;
}
FooAdvertController类:
@RestController
@RequestMapping("/api")
public class FooAdvertController {
@Autowired
private FooAdvertService fooAdvertService;
@GetMapping("count")
public ResponseEntity getAdvertsCount() {
Long advertsCount = fooAdvertService.count();
return new ResponseEntity(advertsCount, HttpStatus.OK);
}
}
尝试运行我的应用时出现错误消息:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fooAdvertController': Unsatisfied dependency expressed through field 'fooAdvertService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fooAdvertService': Unsatisfied dependency expressed through field 'genericDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooAdvertDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class org.wixanz.rm.ads.domain.FooAdvert
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fooAdvertService': Unsatisfied dependency expressed through field 'genericDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooAdvertDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class org.wixanz.rm.ads.domain.FooAdvert
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooAdvertDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class org.wixanz.rm.ads.domain.FooAdvert
Caused by: java.lang.IllegalArgumentException: Not a managed type: class org.wixanz.rm.ads.domain.FooAdvert
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
答案 0 :(得分:0)
尝试按名称
自动装配IFooAdvertDao@Service
public class FooAdvertService extends GenericServiceImpl<FooAdvert, String>
{
@Autowired
@Qualifier("fooAdvertDao")
private IFooAdvertDao fooAdvertDao;
}