不是使用@IdClass创建托管类型和复合ID

问题描述 投票:0回答:3

我想用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/

java spring hibernate
3个回答
0
投票

只看一下给定的例子,你似乎错过了两件事:

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

希望有所帮助。


0
投票

根据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


0
投票

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

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

}

并不是

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

}
© www.soinside.com 2019 - 2024. All rights reserved.