休眠多对多like子句
Posted
技术标签:
【中文标题】休眠多对多like子句【英文标题】:Hibernate many to many like clause 【发布时间】:2017-03-19 14:59:41 【问题描述】:电影
@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】:使用有效的 JPQL,并始终指定一个选择子句:
select distinct m from MovieEntity ...
否则,如您所见,查询返回List<Object[]>
,其中每个数组包含电影和流派。
【讨论】:
以上是关于休眠多对多like子句的主要内容,如果未能解决你的问题,请参考以下文章