无法解析属性:product_id:评论

时间:2017-04-04 18:41:42

标签: spring hibernate spring-data

在服务器启动时获取错误查询,该查询查找用户发布的有关特定产品的所有评论。

引起:java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:product_id of:haughton.dvdstore.model.Comment [select c from haughton.dvdstore.model.Comment c其中c.product_id = :ID]

回购课程

@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 Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "product_id")
private Product product;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

private String text;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}
}

我的产品类

@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;
}
}

1 个答案:

答案 0 :(得分:1)

应该是@Query(“从评论c中选择c,其中c.product.id =:id”)