多对多查询存在一些问题。这是我数据库的一部分
这是我的关系类的代码:
@Entity
@Table(name = "notification_has_user", catalog = "ats")
@AssociationOverrides({
@AssociationOverride(name = "pk.user",
joinColumns = @JoinColumn(name = "id_username")),
@AssociationOverride(name = "pk.notification",
joinColumns = @JoinColumn(name = "id_notification")) })
public class NotificationHasUser implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private NotificationHasUserKeys pk = new NotificationHasUserKeys();
private boolean isRead;
@EmbeddedId
public NotificationHasUserKeys getPk() {
return pk;
}
/**
* @param pk the pk to set
*/
public void setPk(NotificationHasUserKeys pk) {
this.pk = pk;
}
@Transient
public User getUser() {
return getPk().getUser();
}
public void setStock(User user) {
getPk().setUser(user);
}
@Transient
public Notification getNotification() {
return getPk().getNotification();
}
public void setCategory(Notification notification) {
getPk().setNotification(notification);
}
@Column(name = "isRead", nullable = false)
@Type(type = "org.hibernate.type.NumericBooleanType")
public boolean isRead() {
return isRead;
}
/**
* @param isRead the isRead to set
*/
public void setRead(boolean isRead) {
this.isRead = isRead;
}
..equal and hash code
}
现在,我需要检索所有未读消息的列表,因此所有具有isRead = 0字段的通知。
我已经尝试过帖春查询
List<Notification> findByPkUserAndIsReadFalse(Useruser);
并与此:
@Query("SELECT c FROM NotificationHasUser c WHERE c.pk.user = ?1 AND c.isRead=false")
但是字段isRead给我带来了问题和此错误:
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [isRead] on this ManagedType [com.domain.NotificationHasUser]
at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:144) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:130) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:475) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:199) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:146) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.and(JpaQueryCreator.java:96) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.and(JpaQueryCreator.java:44) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109) ~[spring-data-commons-1.9.4.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) ~[spring-data-commons-1.9.4.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) ~[spring-data-commons-1.9.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:110) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:230) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:64) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94) ~[spring-data-jpa-1.7.4.RELEASE.jar:na]
... 56 common frames omitted
您能解释一下错误在哪里吗?
最佳答案
将boolean
字段重命名为read
并使用isRead
和setRead
作为其获取器和设置器,这样它就更符合Java Beans规范。