Java Jpa Query searching an Entityes Related By Id

215 Views Asked by At

I've 2 entities: Author and Album. Author is OneToMany(mappedby="author") Album is ManyToOne

I'm trying to search Album where idAuthor is passed.

Here the code:

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

And here there is Author Entity

@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 + "]";
}

}

But I receive this: org.apache.openjpa.persistence.ArgumentException: No metadata was found for type "class java.lang.Integer". The class is not enhanced.
Why?

1

There are 1 best solutions below

5
On BEST ANSWER

You're trying to send Integer values to object

you says author (object) like id (idAuthor Long)

Correct as follows try

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