我试图理解并找出以下用例的解决方案
这些是我的实体类
用户
@Entity
@Table(name = "USER")
public class User {
private UserID id;
private Set<UserAddress> addresses = new HashSet<UserAddress>(0);
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "userId", column = @Column(name = "USER_ID", nullable = false, length = 32)),
@AttributeOverride(name = "userType", column = @Column(name = "USER_TYPE", nullable = false, precision = 12, scale = 0)) })
public User getId() {
return this.id;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade={CascadeType.ALL})
@BatchSize(size=50)
public Set<UserAddress> getAddresses() {
return this.addresses;
}
........
}
用户地址
@Entity
@Table(name = "USERADDRESS")
public class UserAddress {
private UserID id;
Private User user;
private String address;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "userId", column = @Column(name = "USER_ID", nullable = false, length = 32)),
@AttributeOverride(name = "userType", column = @Column(name = "USER_TYPE", nullable = false, precision = 12, scale = 0)) })
public User getId() {
return this.id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "userId", referencedColumnName = "USER_ID", nullable = false, insertable=false, updatable=false),
@JoinColumn(name = "userType", referencedColumnName = "USER_TYPE", nullable = false, insertable=false, updatable=false) })
public User getUser() {
return this.user;
}
........
}
用户身份
@Embeddable
public class UserId implements Serializable {
private String userNo;
private Long UserType;
.......
.......
}
我已经为User,UserID和UserAddress创建了staticmetamodel类,并根据Specification创建了查询。
用户的元模型类
@StaticMetamodel(User.class)
public abstract class User_ {
public static volatile SetAttribute<User, UserAddress> addresses;
public static volatile SingularAttribute<User, UserID> id;
}
UserId的元模型
@StaticMetamodel(UserID.class)
public abstract class UserID_ {
public static volatile SingularAttribute<UserID, String> userNo;
public static volatile SingularAttribute<UserID, Long> UserType;
}
我正在尝试检索按UserType排序并根据userId搜索的最多10个User对象。该查询必须检索UserAddresses作为预先获取。
我的规范对象是
用户规格
public class UserSpecifications {
public static Specification<User> userNoIs(String userNo) {
return (root, query, cb) -> {
root.fetch(User_.addresses);
return cb.equal(root.get(User_.id).get(UserID_.userNo),userNo);
};
}
}
DAO功能:
Sort sortInstructions = new Sort(Sort.Direction.DESC, "id.userNo");
Specifications<User> specifications = Specifications.where(userNoIs(input.getUserNo()));
List<User> userList = userRepository.findAll(specifications,sortInstructions);
我收到以下异常
java.lang.ClassCastException: org.hibernate.jpa.internal.metamodel.SingularAttributeImpl$Identifier cannot be cast to javax.persistence.metamodel.ManagedType
at org.hibernate.jpa.criteria.path.AbstractFromImpl.locateManagedType(AbstractFromImpl.java:139)
at org.hibernate.jpa.criteria.path.AbstractFromImpl.locateAttributeInternal(AbstractFromImpl.java:133)
at org.hibernate.jpa.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:221)
at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:194)
at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:287)
at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:261)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:441)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:294)
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:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:322)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
最佳答案
问题在于root.fetch(User_.userAddresses);
-在您的User
类中,您具有addresses
而不是userAddresses
,因此您可能为此错误地生成了元模型。
解决的另一件事应该是ListAttribute
而不是SingularAttribute
。
更新编辑:
尝试使用更新的版本,您描述的问题已在spring-data 1.5中解决