代码之家  ›  专栏  ›  技术社区  ›  a.moussa

不是托管类型,创建并组合id为@IdClass

  •  0
  • a.moussa  · 技术社区  · 8 年前

    我想用spring创建两个类,以便拥有IdClass。 我试过这个:

    import java.persistence.*;
    
    public class InfosId extends Serializable {
    
        private String name;
    
        private String age;
    
        public Infos() { }
    
        public Infos(String name) {
            this.name= name;
        }
    
        public Infos(String name, String age) {
            this.age = age;
            this.name= name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name= name;
        }
    
        public String getAge() { return age; }
    
        public void setAge(String age) { this.age= age;}
    
        @Override
        public boolean equals(Object other) {
            return (this == other) ||  this.name.equals( ((Infos) other).name  );
        }
    
        @Override
        public int hashCode() {
            return super.hashCode();
        }
    
    }
    

    我想使用第一个类InfosId作为以下类的属性:

    @Entity
    @IdClass(InfosId.class)
    @Table(name = "infos")
    @EntityListeners(Infos .class)
    public class Infos {
    
        @Id
        @Column(name = "name")
        private String name;
    
        @Id
        @Column(name = "age")
        private String age;
    
        public Infos() { }
    
        public Infos(String name) {
            this.name= name;
        }
    
        public Infos(String name, String age) {
            this.age = age;
            this.name= name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name= name;
        }
    
        public String getAge() { return age; }
    
        public void setAge(String age) { this.age= age;}    
    }    
    

    不幸的是,当我执行代码时,我获得了以下异常:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiInController': Unsatisfied dependency expressed through field 'accessPointService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accessPointService': Unsatisfied dependency expressed through field 'consumer'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'consumerService': Unsatisfied dependency expressed through field 'infosRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infosRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.test.datalake.model.entity.infosId
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        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:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
        at com.test.datalake.ApiInApplication.main(ApiInApplication.java:12)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accessPointService': Unsatisfied dependency expressed through field 'consumer'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'consumerService': Unsatisfied dependency expressed through field 'infosRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infosRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.test.model.entity.InfosId
    

    你知道我为什么会例外吗?

    我试着按照这篇文章的指示: http://www.thejavageek.com/2014/05/01/jpa-idclass-example/

    3 回复  |  直到 8 年前
        1
  •  0
  •   SlideM    8 年前

    仅看给出的示例,您似乎遗漏了两件事:

    1. @IdClass(InfosId.class)而不是@IdClass(InfosId)
    2. id类中缺少Serializable

    希望这有帮助。

        2
  •  0
  •   aggredi    8 年前

    根据hibernate文档,您必须添加getId和setId方法。

        public InfosId getId() {
            return new InfosId (
                name, age
            );
        }
    
        public void setId(InfosId id) {
            this.name= id.getName();
            this.age = id.getAge();
        }
    

    类似这样:

    @Entity
    @IdClass(InfosId.class)
    @Table(name = "infos")
    @EntityListeners(Infos .class)
    public class Infos {
    
        @Id
        @Column(name = "name")
        private String name;
    
        @Id
        @Column(name = "age")
        private String age;
    
        public Infos() { }
    
        public Infos(String name) {
            this.name= name;
        }
    
        public Infos(String name, String age) {
            this.age = age;
            this.name= name;
        }
    
        public InfosId getId() {
            return new InfosId (
                name, age
            );
        }
    
        public void setId(InfosId id) {
            this.name= id.getName();
            this.age = id.getAge();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name= name;
        }
    
        public String getAge() { return age; }
    
        public void setAge(String age) { this.age= age;}    
    }   
    

    文档 http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite-nonaggregated

        3
  •  0
  •   a.moussa    8 年前

    我的错是,我给了类InfosId,尽管我的类InfosRepository中有Infos。我现在有:

    @Repository
    public interface InfoRepository extends JpaRepository<Infos, String> {
    
    }
    

    而不是

    @Repository
    public interface InfoRepository extends JpaRepository<InfosID, String> {
    
    }