我有以下课程:
@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);
}
当我初始化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
问题: Spring Data JPA是否不支持对主键的子集进行查询? 还是我需要为此实现自定义存储库方法?
好的,我还尝试使用以下方法执行此操作@IdClass
:
@Entity
@Table(name = "my_entity")
@JsonIgnoreProperties(ignoreUnknown = true)
@IdClass(Pk.class)
public class MyEntity {
@Id private Integer type;
@Id private String userId;
public Pk getId() {
return id;
}
public void setId(Pk id) {
this.id = id;
}
}
@IdClass(Pk.class)
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;
}
}
这确实给了我一个不同的错误消息a NullPointerException
,这向我暗示Spring Data
JPA无法为构建查询findAllByUserId(...)
。我改为对该查询方法进行了自定义实现:
public interface MyEntityRepository extends JpaRepository<MyEntity, Pk>, MyEntityRepositoryCustom {
}
public interface MyEntityRepositoryCustom {
List<MyEntity> findAllByUserId(String userId);
}
public class MyEntityRepositoryImpl implements MyEntityRepositoryCustom {
@PersistenceContext
private EntityManager em;
@Override
public List<MyEntityRepositoryCustom> findAllByUserId(String userId) {
return em
.createQuery("select o from MyEntity o where o.userId=:userId",
MyEntity.class).setParameter("userId", userId).getResultList();
}
}
…而且, 它起作用了!
问题内容: 给定具有复合主键的表,是否存在用于编写查询的合法语法,例如: 如果这是不可能的,并且您无法修改架构,那么如何执行上述等效操作? 我还将在此处使用术语“复合主键”,“子选择”,“子选择”和“子查询”,以表示这些别名的搜索结果。 编辑 :我对标准SQL的答案以及将与PostgreSQL和SQLite 3一起使用的答案感兴趣。 问题答案: 更换你的。 或创建您的临时表并用它代替..
根据此链接: DynamoDB上支持的操作 “只能查询具有复合主键(分区键和排序键)的表。” 但这似乎并不正确。我在DynamoDB中有一个名为“users”的表,该表的主键只包含一个属性“username”。 而且我能够在NodeJS中仅使用“username”属性上的“KeyConditionExpression”查询该表。请参见以下内容: } 这段代码运行得很好。所以我想知道文档是否有误,或
spring jpa 针对复合主键如何通过 @Query 查询? 上面代码ide会提示无法解析的字段 id.userId。我的 UserRecommendedEntity 代码如下: 无
我有以下表格,如何将它们映射到JPA实体: 事件表与会议表具有一对多的关系。我如何在JPA中映射这种双向关系?
问题内容: 我正在设计一个数据库,该数据库将用于存储来自许多不同来源的数据。我存储的实例由原始来源分配了唯一的ID。我存储的每个实例都应包含有关其来源的信息,以及与此来源相关联的ID。 作为示例,请考虑说明该问题的下表: 请注意,尽管每个来源的唯一,但有可能在不同来源中找到相同的来源。 我对关系数据库有一个不错的了解,但是与专家甚至是经验丰富的用户都相去甚远。我在此设计中面临的问题是应该用作主键。
问题内容: 我在这里搜索,但未找到任何类似的主题,因此我发布了一个新问题。 我正在使用现有数据库上的Hibernate。我们不允许更改表的结构和数据。该应用程序正在从数据库读取数据,并根据某种逻辑迁移到另一个数据存储。 现在的问题是关于复合PK映射。例如 表A具有复合PK。 表B也有一个复合PK,此复合PK的一部分是A的PK,此处也用作FK。 我尝试了几种方法,但都无济于事。谁能告诉一个有效的Hi