Java Jpa查询搜索与Id相关的实体

时间:2015-06-17 11:46:06

标签: java jpa jdbc entity

我有2个实体:作者和专辑。 作者是OneToMany(mappedby =“author”) 专辑是ManyToOne

我正在尝试搜索传递了idAuthor的相册。

这里是代码:

public Collection<Album> findByIdAuthor(Long idAuthor, HttpServletRequest request) {
    Collection<Album> album;
    try {
        album = em.createQuery("select a from Album a where a.author like :id").setParameter("id", idAuthor).getResultList();
    } catch(Exception e) {
        request.setAttribute("err", e.toString());
        return null;
    }
    return album;
}

这里有作者实体

@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(nullable=false)
private String name;

@Column
private int debut;

@Column(length=1000)
private String info;

@OneToMany(mappedBy="author")
private List<Track> tracks;

@OneToMany(mappedBy="author",cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
private List<Album> album;

public Author() {
    super();
}

public Author(String name) {
    super();
    this.name = name;
}

public Long getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getDebut() {
    return debut;
}

public void setDebut(int debut) {
    this.debut = debut;
}

public String getInfo() {
    return info;
}

public void setInfo(String info) {
    this.info = info;
}

public List<Track> getTracks() {
    return tracks;
}

public void setTracks(List<Track> tracks) {
    this.tracks = tracks;
}

public List<Album> getAlbum() {
    return album;
}

public void setAlbum(List<Album> album) {
    this.album = album;
}

@Override
public String toString() {
    return "Autore [id=" + id + ", nome=" + name + ", dataDebutto="
            + debut + ", info=" + info + ", brani=" + tracks
            + ", album=" + album + "]";
}

}

但是我收到了这个:org.apache.openjpa.persistence.ArgumentException:没有找到类型“class java.lang.Integer”的元数据。课程没有增强 为什么呢?

1 个答案:

答案 0 :(得分:0)

您正在尝试将Integer值发送到对象

你说作者(对象)喜欢id(idAuthor Long)

如下所示正确尝试

public Collection<Album> findByIdAuthor(Long idAuthor, HttpServletRequest request) {
    Collection<Album> album;
    try {
        album = em.createQuery("select a from Album a where a.author.id like :id").setParameter("id", idAuthor).getResultList();
    } catch(Exception e) {
        request.setAttribute("err", e.toString());
        return null;
    }
    return album;
}