我想用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/
答案 0 :(得分:0)
看看给定的例子,你似乎错过了两件事:
希望有所帮助。
答案 1 :(得分: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;}
}
答案 2 :(得分:0)
我的错,尽管我的班级InfosRepository里面有Infos,但我还是给了InfosId课程。我现在有:
@Repository
public interface InfoRepository extends JpaRepository<Infos, String> {
}
而不是
@Repository
public interface InfoRepository extends JpaRepository<InfosID, String> {
}