public class Media implements java.io.Serializable {
private int id;
private MediaKind mediaKind;
private String name;
private byte[] cover;
private Date releaseDate;
private Integer contentRating;
private String summary;
private Set mediaCrews = new HashSet(0);
private Set mediaInstances = new HashSet(0);
private Set ratings = new HashSet(0);
private Set genres = new HashSet(0);
static SessionFactory mediaFactory=Main.config.buildSessionFactory();
public Media() {
}
public Media(int id, MediaKind mediaKind, String name) {
this.id = id;
this.mediaKind = mediaKind;
this.name = name;
Session mediaSession = mediaFactory.getCurrentSession();
}
public Media(int id, MediaKind mediaKind, String name, byte[] cover,
Date releaseDate, Integer contentRating, String summary,
Set mediaCrews, Set mediaInstances, Set ratings, Set genres) {
this.id = id;
this.mediaKind = mediaKind;
this.name = name;
this.cover = cover;
this.releaseDate = releaseDate;
this.contentRating = contentRating;
this.summary = summary;
this.mediaCrews = mediaCrews;
this.mediaInstances = mediaInstances;
this.ratings = ratings;
this.genres = genres;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public MediaKind getMediaKind() {
return this.mediaKind;
}
public void setMediaKind(MediaKind mediaKind) {
this.mediaKind = mediaKind;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getCover() {
return this.cover;
}
public void setCover(byte[] cover) {
this.cover = cover;
}
public Date getReleaseDate() {
return this.releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public Integer getContentRating() {
return this.contentRating;
}
public void setContentRating(Integer contentRating) {
this.contentRating = contentRating;
}
public String getSummary() {
return this.summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public Set getMediaCrews() {
return this.mediaCrews;
}
public void setMediaCrews(Set mediaCrews) {
this.mediaCrews = mediaCrews;
}
public Set getMediaInstances() {
return this.mediaInstances;
}
public void setMediaInstances(Set mediaInstances) {
this.mediaInstances = mediaInstances;
}
public Set getRatings() {
return this.ratings;
}
public void setRatings(Set ratings) {
this.ratings = ratings;
}
public Set getGenres() {
return this.genres;
}
public void setGenres(Set genres) {
this.genres = genres;
}
public static java.util.List<Media> search(String name){
java.util.List list;
Session sess=mediaFactory.getCurrentSession();
sess.beginTransaction();
Criteria criteria=sess.createCriteria(Media.class);
criteria.add(Restrictions.like("name", "%"+name+"%"));
list= criteria.list();
Hibernate.initialize(list);
sess.getTransaction().commit();
//connection=sess.close();
return list;
}
public ArrayList<MediaInstance> availableInstances(){
//sess.beginTransaction();
Session sess=mediaFactory.openSession();
sess.beginTransaction();
Criteria criteria=sess.createCriteria(MediaInstance.class);
criteria.add(Restrictions.like("media", name));
sess.getTransaction().commit();
return (ArrayList<MediaInstance>)criteria.list();
/*MediaInstance[] instances=(MediaInstance[]) mediaInstances.toArray();
ArrayList<MediaInstance> mediaInstanceList=new ArrayList<MediaInstance>(Arrays.asList(instances));
for(int i=0;i<mediaInstanceList.size();i++){
MediaInstance instance=mediaInstanceList.get(i);
if(!instance.isAvailable()|!instance.isSellable()){
mediaInstanceList.remove(instance);
}
}
//sess.getTransaction().commit();
//sess.close();
return mediaInstanceList;*/
}
}
这是我的第二类mediaInstance:
public class MediaInstance implements java.io.Serializable {
private int id;
private MediaType mediaType;
private Media media;
private String price;
private boolean available;
private boolean sellable;
private Set rents = new HashSet(0);
private Set purchases = new HashSet(0);
public MediaInstance() {
}
public MediaInstance(int id, MediaType mediaType, Media media,
String price, boolean available, boolean sellable) {
this.id = id;
this.mediaType = mediaType;
this.media = media;
this.price = price;
this.available = available;
this.sellable = sellable;
}
public MediaInstance(int id, MediaType mediaType, Media media,
String price, boolean available, boolean sellable, Set rents,
Set purchases) {
this.id = id;
this.mediaType = mediaType;
this.media = media;
this.price = price;
this.available = available;
this.sellable = sellable;
this.rents = rents;
this.purchases = purchases;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public MediaType getMediaType() {
return this.mediaType;
}
public void setMediaType(MediaType mediaType) {
this.mediaType = mediaType;
}
public Media getMedia() {
return this.media;
}
public void setMedia(Media media) {
this.media = media;
}
public String getPrice() {
return this.price;
}
public void setPrice(String price) {
this.price = price;
}
public boolean isAvailable() {
return this.available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public boolean isSellable() {
return this.sellable;
}
public void setSellable(boolean sellable) {
this.sellable = sellable;
}
public Set getRents() {
return this.rents;
}
public void setRents(Set rents) {
this.rents = rents;
}
public Set getPurchases() {
return this.purchases;
}
public void setPurchases(Set purchases) {
this.purchases = purchases;
}
}
当我尝试在方法criteria.list()
中调用availableInstances()
时,我得到了此异常。
May 27, 2014 1:22:56 PM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: Media, getter method of property: id
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of Media.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:192)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:293)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1994)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1965)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1900)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1861)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
at MediaInstance.availableInstances(MediaInstance.java:125)
at Main.main(Main.java:24)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)
... 23 more
我使用JBoss工具从我的数据库生成类和映射文件。 所以我不认为问题出在映射文件中。
答案 0 :(得分:1)
您的条件查询似乎没有问题。您正尝试按媒体实体的实例进行过滤,但将名称作为参数传递。
尝试这样的事情:
criteria.add(Restrictions.eq("media", this));
答案 1 :(得分:0)
第二个参数传递给静态&#34;喜欢&#34; Restrictions静态类的方法(或&#34; eq&#34;方法等)必须与第一个字符串(第一个参数)标识的字段的类型相同。 在您的示例中:
criteria.add(Restrictions.like("media", name));
&#34;名称&#34;是一个字符串而#34;媒体&#34; MediaInstance类的字段是Media的实例。你应该替换&#34; name&#34;使用媒体实例,甚至媒体实例本身也在执行&#34; availableInstances&#34;方法,所以:
criteria.add(Restrictions.like("media", this));