关于jpa dao接口的自动实现

jackyCoding 发布于 2013/11/22 12:23
阅读 3K+
收藏 0
最近在搞HIbernate,spring,jpa的整合,感觉jpa dao 接口的自动实现用起来很方便,但是有一点疑问的地方;

dao接口的定义说是要这样



public interface UserDao extends JpaRepository<Persion, Integer> {
}




public interface  MyDao<T,ID extends Serializable> extends JpaRepository<T,ID> {


}



Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDao': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 28 more
Caused by: java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
at org.hibernate.ejb.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:200)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:61)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:146)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:84)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:67)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:136)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 38 more


加载中
0
东胜神洲
东胜神洲

public interface UserDao extends JpaRepository<Persion, Integer>

这是必须的。。因为 Spring-DATA 要从 JpaRepository 那里拿到 Persion 这个实体类)。。没有这个 Persion,后面根本就玩不动。。。


小昭归来
小昭归来
回复 @jackyCoding : 请问您这个问题解决了没有呢?我也遇到了,如果我不用泛型就正确,用泛型代替后,用@Autowired private BaseRepository<User, Integer> br获取报错--no an mananged type
东胜神洲
东胜神洲
回复 @jackyCoding : 这两天偶然看到 一篇博文,对你应该有参考意义。。http://hejiantx.blog.163.com/blog/static/61867342013691040295/
东胜神洲
东胜神洲
回复 @jackyCoding : 无论你怎么个泛法。。你都要给终端类(最终的 DAO)的父类(或接口)的泛型写上个具体的类型,就如 UserDao 要在接口 JapRepository 加上 Persion 一样。。否则,Spring-DATA 就无法得到 Entity,得不到 Entity ,后面就玩不来。。。建议你要么研究 Spring DATA 源代码,看能否找到方法。。
jackyCoding
jackyCoding
我如果想用泛型来代替person,来写ig公共接口呢,怎么实现
返回顶部
顶部