【发布时间】:2019-06-19 18:21:40
【问题描述】:
我有以下模型和存储库:
@Entity
@Table(name = "db_user", uniqueConstraints = { @UniqueConstraint(columnNames = "email") })
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_user")
@SequenceGenerator(name = "seq_user", sequenceName = "seq_user")
@Column(name = "id")
private Long id;
// ...
}
@Entity
@Table(name = "movie")
public class Movie extends AbstractItem {
// Id column inherited from AbstractItem
// ...
}
@Entity
@Table(name = "movie_user")
public class MovieOwnership extends AbstractOwnership {
@ManyToOne
private Movie movie;
// ...
}
@MappedSuperclass
public abstract class AbstractOwnership{
@Id
@SequenceGenerator(name = "seq_default", sequenceName = "seq_default")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_default")
@Column(name = "id")
private Long id;
@ManyToOne
private User owner;
// ...
}
public interface MovieOwnershipRepository extends QueryDslJpaRepository<MovieOwnership, Long> {
List<MovieOwnership> findByOwnerId(Long ownerId);
MovieOwnership findByOwnerIdAndMovie(Long ownerId, Movie movieId);
List<MovieOwnership> findByOwnerIdAndMovieIdIn(Long ownerId, Set<Long> movieIds);
}
我正在尝试使用 Spring 的 findBy 请求来按所有者或电影获取 MovieOwnership,使用两个实体的 id 字段。我可以直接使用所有者的 id,但是在我的请求中使用 MovieId 似乎是错误的(尽管我可以使用整个 Movie 对象)。在上面的代码中,前两个 findBy 没问题,但最后一个抛出这个异常:
原因:java.lang.IllegalArgumentException:无法定位 此 ManagedType 上具有给定名称 [movieId] 的属性 [carrm.app.data.AbstractOwnership]
如果我尝试使用 Movie 中的另一个属性(如 findByMovieTitle),它会编译,但我无法让它在 id 上工作。
知道如何解决这个问题吗?
【问题讨论】:
-
movieId 属性在哪里定义?
-
这三种方法你调用的是哪一种?
-
这段代码还能编译吗?你有一个扩展类的接口(QuerydslJpaRepository)
-
@XavierBouclet movieId 属性是指MovieOwnership 的movie 字段的id 字段,就像ownerId 是指User 的id 属性一样。
-
@SimonMartinelli 我成功调用了前两种方法,但添加第三种方法会破坏应用程序。
标签: spring-boot jpa spring-data-jpa