2016-05-09 12 views
2

Ich versuche, meine db-Tabellen über Spring-Boot durch verschiedene Spring-Tutorials zu offenbaren und ich bin in folgende Ausnahme (Ich werde Ausnahme am Ende posten).Spring Daten: Nicht ein verwalteter Typ: Klasse java.lang.Object

Hier die Klassen, die ich

package com.xxx.xxx.db; 

import org.springframework.data.repository.Repository; 

import java.io.Serializable; 

public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID>{ 
    T findOne(ID id); 
    T save(T entity); 
} 

bin mit Und hier ist meine spezifischen Dienstklasse

package com.xxx.xxx.tablename; 

import com.xxx.xxx.db.BaseRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

@Service 
public class TableNameService { 

    @Autowired 
    private BaseRepository<TableName, Long> repository; 

    public TableName findById(Long id){ 
    return repository.findOne(id); 
    } 
} 

und hier ist meine Einheit

package com.xxx.xxx.tablename; 

import lombok.Getter; 
import lombok.Setter; 

import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 

@Getter 
@Setter 
@Entity 
@Table(name="table_name") 
public class TableName { 

    @Id 
    private long id; 

    @NotNull 
    private String name; 

    public TableName() { 
    } 

    public TableName(long id){ 
    this.id = id; 
    } 

    public TableName(String name){ 
    this.name = name; 
    } 

    public TableName(long id, String name){ 
    this.id = id; 
    this.name = name; 
    } 
} 

Meine Federkonfigurationen sein werden getan von application.yml Datei. Die Datei hat derzeit nur database Verbindungsinformationen und nichts mehr als das

Ich habe auch eine Klasse mit dem Namen BeansConfig, die von @Configuration annotiert ist. Diese Klasse wird jedoch verwendet, um zu validieren, ob db-Eigenschaften, die von application.yml bereitgestellt werden, gültig sind.

Und jetzt ist hier die Ausnahme

springframework.beans.factory.BeanCreationException: Error creating bean with name 'TableNameService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.xxx.db.BaseRepository com.xxx.xxx.tablename.TableNameService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) 
    at com.xxx.xxx.Application.main(Application.java:12) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.xxx.db.BaseRepository com.xxx.xxx.tablename.TableNameService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    ... 17 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1199) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) 
    ... 19 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object 
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219) 
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68) 
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    ... 29 common frames omitted 
+0

Sie bereits Repository-Schnittstellen von Spring Data nutzen können - zum Beispiel JpaRepository. Versuchen Sie, eine spezifische Schnittstelle für Ihre Table-Klasse zu erstellen - etwa wie "TableRepository extends JpaRepository " und mit der Annotation @Repository zu versehen. – lenach87

Antwort

3

das nicht möglich ist so, wie Sie wollen. Da T eine Entity sein muss, müssen Sie es einschränken. Eine Möglichkeit wäre, ein Repository pro konkrete Entität zu machen, aber hier haben Sie eine allgemeinere Art und Weise ihrer Durchführung:

Verwenden Sie eine Baseclass für Ihre Entitäten:

@Entity 
public abstract class BaseClass<IdType extends Serializable>{ 

    private IdType id; 

    //Getter and setters 

} 

definieren @Repository für Ihre Basisklasse:

@Repository 
public interface Dao<T extends BaseClass, IdType extends Serializable> extends 
     CrudRepository<T, IdType> { 

} 

Sie können die gemeinsam genutzte Einrichtung Operationen in dieser Schnittstelle definieren und von ihnen für konkreter diejenigen in den Fällen fordern erben. auch

Siehe:

+0

Das ist etwas, das ich gerne implementieren würde, aber im Moment habe ich Probleme, ein Repo für eine bestimmte Klasse zu schreiben. Ich habe eine Klasse 'TableNameGatewayRepo erweitert JpaRepoistory ' und ich bekomme immer noch denselben Fehler –

+0

Versuch 'TableNameGatewayRepo erweitert CrudRepository ' oder die gleiche Weise mit 'Jpa' statt 'Crud' –

+0

das funktioniert. Vielen Dank –

Verwandte Themen