Spring 无法解析属性:product\u id:Comment
在服务器启动时获取错误查找用户发布的有关特定产品的所有评论的查询 原因:java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:product_id of:haughton.dvdstore.model.Comment[从haughton.dvdstore.model.Comment c中选择c,其中c.product_id=:id] 回购类Spring 无法解析属性:product\u id:Comment,spring,hibernate,spring-data,Spring,Hibernate,Spring Data,在服务器启动时获取错误查找用户发布的有关特定产品的所有评论的查询 原因:java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:product_id of:haughton.dvdstore.model.Comment[从haughton.dvdstore.model.Comment c中选择c,其中c.product_id=:id] 回购类 @Repository public interface Comme
@Repository
public interface CommentDao extends CrudRepository<Comment,Long> {
@Query("select c from Comment c where c.product_id = :id")
List<Comment> allCommentsByProductId(@Param("id") Long id);
}
我的产品类别
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
private int quantityInStock;
Date date;
private double price;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getQuantityInStock() {
return quantityInStock;
}
public void setQuantityInStock(int quantityInStock) {
this.quantityInStock = quantityInStock;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
用户类
@Entity
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
@Size(min = 8, max = 20)
private String username;
@Column(length = 100)
private String password;
@Column(nullable = false)
private boolean enabled;
@OneToOne
@JoinColumn(name = "role_id")
private Role role;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(role.getName()));
return authorities;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
public static PasswordEncoder getPasswordEncoder() {
return PASSWORD_ENCODER;
}
}
它应该是@Queryselect c from Comment c,其中c.product.id=:id
@Entity
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
@Size(min = 8, max = 20)
private String username;
@Column(length = 100)
private String password;
@Column(nullable = false)
private boolean enabled;
@OneToOne
@JoinColumn(name = "role_id")
private Role role;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(role.getName()));
return authorities;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
public static PasswordEncoder getPasswordEncoder() {
return PASSWORD_ENCODER;
}
}