Zty*_*tyx 4 hibernate spring-data-jpa
我有以下课程:
@Entity
@Table(name = "my_entity")
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyEntity {
@EmbeddedId
private Pk id;
public Pk getId() {
return id;
}
public void setId(Pk id) {
this.id = id;
}
}
@Embeddable
public class Pk implements Serializable {
private static final long serialVersionUID = -3090221844117493661L;
private Integer type;
private String userId;
public Pk() {
}
public Pk(String userId, Integer type) {
this.setUserId(userId);
this.setType(type);
}
public Integer getType() {
return type;
}
public String getUserId() {
return userId;
}
public void setType(Integer type) {
this.type = type;
}
public void setUserId(String userId) {
this.userId = userId;
}
// Auto-generated by Eclipse.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((type == null) ? 0 : type.hashCode());
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}
// Auto-generated by Eclipse.
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pk other = (Pk) obj;
if (type != other.type)
return false;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}
}
public interface MyEntityRepository extends JpaRepository<MyEntity, Pk> {
List<MyEntity> findAllByUserId(String userId);
}
Run Code Online (Sandbox Code Playgroud)
当我初始化Spring Data JPA时,我收到错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEntityRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property user found for type MyEntity!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:615)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
at com.myproject.backend.common.ServiceContext.start(ServiceContext.java:293)
at com.myproject.run(AbstractServiceContainer.java:55)
at com.myproject..run(AbstractServiceContainer.java:1)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:42)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
at io.dropwizard.cli.Cli.run(Cli.java:70)
at io.dropwizard.Application.run(Application.java:72)
at com.myproject..main(CombinedServiceContainer.java:106)
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property user found for type MyEntity!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:213)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:321)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:301)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:85)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:60)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:168)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:320)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
... 18 more
Run Code Online (Sandbox Code Playgroud)
问题:Spring Data JPA不支持查询主键的子集吗?或者我需要为此实现自定义存储库方法吗?
好吧,我也尝试这样做@IdClass:
@Entity\n@Table(name = "my_entity")\n@JsonIgnoreProperties(ignoreUnknown = true)\n@IdClass(Pk.class)\npublic class MyEntity {\n @Id private Integer type;\n @Id private String userId;\n\n public Pk getId() {\n return id;\n }\n\n public void setId(Pk id) {\n this.id = id;\n }\n\n}\n\n@IdClass(Pk.class)\npublic class Pk implements Serializable {\n private static final long serialVersionUID = -3090221844117493661L;\n private Integer type;\n private String userId;\n\n public Pk() {\n }\n\n public Pk(String userId, Integer type) {\n this.setUserId(userId);\n this.setType(type);\n }\n\n public Integer getType() {\n return type;\n }\n\n public String getUserId() {\n return userId;\n }\n\n public void setType(Integer type) {\n this.type = type;\n }\n\n public void setUserId(String userId) {\n this.userId = userId;\n }\n\n // Auto-generated by Eclipse.\n @Override\n public int hashCode() {\n final int prime = 31;\n int result = 1;\n result = prime * result + ((type == null) ? 0 : type.hashCode());\n result = prime * result + ((userId == null) ? 0 : userId.hashCode());\n return result;\n }\n\n // Auto-generated by Eclipse.\n @Override\n public boolean equals(Object obj) {\n if (this == obj)\n return true;\n if (obj == null)\n return false;\n if (getClass() != obj.getClass())\n return false;\n Pk other = (Pk) obj;\n if (type != other.type)\n return false;\n if (userId == null) {\n if (other.userId != null)\n return false;\n } else if (!userId.equals(other.userId))\n return false;\n return true;\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这确实给了我一条不同的错误消息,a NullPointerException,它暗示我 Spring Data JPA 无法为 构建查询findAllByUserId(...)。我改为对该查询方法进行了自定义实现:
public interface MyEntityRepository extends JpaRepository<MyEntity, Pk>, MyEntityRepositoryCustom {\n\n}\n\npublic interface MyEntityRepositoryCustom {\n\n List<MyEntity> findAllByUserId(String userId);\n\n}\n\npublic class MyEntityRepositoryImpl implements MyEntityRepositoryCustom {\n\n @PersistenceContext\n private EntityManager em;\n\n @Override\n public List<MyEntityRepositoryCustom> findAllByUserId(String userId) {\n return em\n .createQuery("select o from MyEntity o where o.userId=:userId",\n MyEntity.class).setParameter("userId", userId).getResultList();\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n...瞧\xc3\xa1,它有效了!
\n| 归档时间: |
|
| 查看次数: |
7419 次 |
| 最近记录: |