Reputation: 3328
I have some problem with many to many query. This is part of my database
and this is the code of my relationship class:
@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
}
Now I need to retrieve the list of all unread messages, therefore all the notifications with field isRead = 0. I have tried with thess Spring query
List<Notification> findByPkUserAndIsReadFalse(Useruser);
and also with this:
@Query("SELECT c FROM NotificationHasUser c WHERE c.pk.user = ?1 AND c.isRead=false")
but the field isRead gives me problem and this error:
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
Could you explain me where is the mistake?
Upvotes: 0
Views: 519
Reputation: 48173
Rename the boolean
field to read
and use isRead
and setRead
as its getter and setter, this way it's more aligned with Java Beans specification.
Upvotes: 1