spring data jpa - 参数值[Book]与预期类型[ContentType]不匹配

时间:2015-12-07 07:03:24

标签: java spring jpa

我有两个简单的表格,contentcontentType

@Entity
@Table(name = "content")
public class Content implements Serializable {

public Content() {}

public Content(String title, String description) {
    this.title = title;
    this.description = description;
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;

@ManyToOne
private ContentCategory contentCategory;

@ManyToOne
private ContentType contentType;

 // getter/setters
}

@Entity
@Table(name = "contentType")
public class ContentType implements Serializable {

public ContentType() {}

public ContentType(String contentType) {
    this.contentType = contentType;
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;

@NotNull
private String contentType;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "contentType")
private Set<Content> content;
`// getter/setters` }

Each content has exactly one type, but many type might be exists in many contents

我要检索类型为Book

的内容

这是我的存储库&#34;

public interface ContentRepository extends JpaRepository<Content, Long> {

    Iterable<Content> findByContentType(String contentType);
}

这是我的测试方法:

@Test
public void retrieve_content_based_on_type() {

    // create and insert a sample content type, i.e. a Book

    ContentType contentType1 = new ContentType("Book");
    contentTypeRepository.save(contentType1);

    //create and insert two contents corresponding to this type
    Content cont1 = new Content("t1", "d1");
    cont1.setContentType(contentType1);
    contentRepository.save(cont1);

    Content cont2 = new Content("t2", "d2");
    cont2.setContentType(contentType1);
    contentRepository.save(cont2);


    //retrieve all contents which their type is Book

    Iterable<Content> allBooks = contentRepository.findByContentType("Book");
    for (Content eachBook : allBooks) {
        System.out.println(eachBook);
    }
}

我遇到了这个例外:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [Book] did not match expected type [com.aa.bb.domain.ContentType (n/a)]; 

nested exception is java.lang.IllegalArgumentException: Parameter value [Book] did not match expected type [com.aa.bb.domain.ContentType (n/a)]

3 个答案:

答案 0 :(得分:2)

您可以将当前方法修改为:

@Query("select c from Content c where c.contentType.contentType = :contentType")
Iterable<Content> findByContentType(String contentType);

原因:Content实体中的contentType属于ContentType类型,而在ContentType实体中属于String类型

就不使用查询注释的Spring Data JPA而言,以下是解决方案:

Iterable<Content> findByContentTypeContentType(String contentType);

Spring Data Reference Link

上述方法适用于Repository类ContentRepository。

答案 1 :(得分:1)

尝试改变:

 public interface ContentRepository extends JpaRepository<Content, Long> 
 {
    Iterable<Content> findByContentType(String contentType);
  }

要:

 public interface ContentRepository extends JpaRepository<ContentType , Long> 
   {
  Iterable<ContentType > findByContentType(String contentType);
    }

答案 2 :(得分:-1)

尝试将变量名称更改为ContentType类中的其他名称。

max-width: 100px;