Hibernate很多很多喜欢的子句

时间:2017-03-19 14:59:41

标签: hibernate orm many-to-many

电影

@Entity
@Table(name = "MOVIE")
public class MovieEntity {
    @Id
    @Column(name = "id")
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    private int id;

    @Column(name = "RELEASE_DATE")
    private Date releaseDate;

    @Column(name = "name")
    private String name;

    @Column(name = "rating")
    private double rating;

    @Column(name = "length")
    private int length;

    @Column(name = "casting")
    private String casting;

    @Column(name = "director")
    private String director;

    @Column(name = "description")
    private String description;

    @Column(name = "writer")
    private String writer;

    @ManyToMany(
            cascade = {CascadeType.PERSIST, CascadeType.MERGE}
    )
     @LazyCollection(LazyCollectionOption.FALSE)
     @JoinTable(name = "CATEGORY",
     joinColumns = {
         @JoinColumn(
             name = "MOVIE_ID", 
             referencedColumnName = "id"
         )
     },
     inverseJoinColumns = {
         @JoinColumn(
             name = "GENRE_ID", 
             referencedColumnName = "id"
         )
     }
 )
    private List<GenreEntity> genre = new ArrayList<>();

    @ManyToMany(
            cascade = {CascadeType.PERSIST, CascadeType.MERGE}
    )
    @LazyCollection(LazyCollectionOption.FALSE)
     @JoinTable(name = "MOVIE_REPLY",
     joinColumns = {
         @JoinColumn(
             name = "MOVIE_ID", 
             referencedColumnName = "id"
         )
     },
     inverseJoinColumns = {
         @JoinColumn(
             name = "REPLY_ID", 
             referencedColumnName = "id"
         )
     }
 )
    private List<ReplyEntity> reply = new ArrayList<>();

    public List<ReplyEntity> getReply() {
        return reply;
    }

    public List<GenreEntity> getGenre() {
        return genre;
    }

    public MovieEntity(){

    }

    public int getId() {
        return id;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getName() {
        return name;
    }

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

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getCasting() {
        return casting;
    }

    public void setCasting(String casting) {
        this.casting = casting;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }

}

类型

@Entity
@Table(name = "GENRE")
public class GenreEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator="increment2")
    @GenericGenerator(name="increment2", strategy = "increment") 
    private int id;

    @Column(unique = true,name="name")
    private String name;

    public int getId() {
        return id;
    }

    @ManyToMany(mappedBy = "genre")
    private List<MovieEntity> movies = new ArrayList<>();

    public GenreEntity( String string){
        this.name=string;
    }

    public GenreEntity(){

    }

    public String getName() {
        return name;
    }

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

}

这是我用来按电影名称搜索的内容

movies = session.createQuery("FROM MovieEntity m where m.name like :name order by m.id ASC")
                            .setParameter("name", "%"+input+"%")
                    .list();

它确实有效。但是当我尝试按照这样的流派名称进行搜索时:

movies = session.createQuery("FROM MovieEntity m join m.genre g where g.name like '%Ale%'").list();

它不起作用。我获得了页面上显示的核心数量的电影但没有属性(名称,流派,导演都是空的)。 如果我试图像这样循环它们:

for(MovieEntity movie:movies){
            System.out.print(movie.getName());
        }

我明白了:

  [Ljava.lang.Object; cannot be cast to MovieEntity

1 个答案:

答案 0 :(得分:1)

使用有效的JPQL,并始终指定select子句:

select distinct m from MovieEntity ...

否则,正如您所看到的,查询返回List<Object[]>,其中每个数组都包含一个电影和一个类型。