简体   繁体   English

Hibernate:IllegalArgumentException:在类中:…属性的getter方法:id

[英]Hibernate:IllegalArgumentException : in class: … getter method of property: id

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

}

here is my second class mediaInstance: 这是我的第二类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;
    }

}

When I try to call criteria.list() in method availableInstances() I get this exception. 当我尝试在方法availableInstances()调用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

I used JBoss tools to generate the classes and mapping files from my database. 我使用JBoss工具从数据库中生成类和映射文件。 so I don't think that the problem is in mapping files. 所以我认为问题不在于映射文件。

Your criteria query doesn't seem right. 您的条件查询似乎不正确。 You are trying to filter by an instance of media entity but passing the name as parameter. 您尝试按媒体实体的实例进行过滤,但将名称作为参数传递。

Try something like this: 尝试这样的事情:

criteria.add(Restrictions.eq("media", this));

The second argument passed to the static "like" method (or "eq" method, and so on) of the Restrictions static class must be of the same type of the field identified by the first string (first argument). 传递给Restrictions静态类的静态“ like”方法(或“ eq”方法等)的第二个参数必须与第一个字符串(第一个参数)标识的字段的类型相同。 In your example: 在您的示例中:

criteria.add(Restrictions.like("media", name));

"name" is a String while "media" field of the MediaInstance class is a instance of Media. “名称”是一个字符串,而MediaInstance类的“媒体”字段是Media的实例。 You should substitute "name" with a media instance, even the Media instance itself executing the "availableInstances" method, so: 您应该用媒体实例替换“名称”,甚至是媒体实例本身执行“ availableInstances”方法,因此:

criteria.add(Restrictions.like("media", this));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类:model.Category,属性:id的getter方法中的IllegalArgumentException - IllegalArgumentException in class: model.Category, getter method of property: id IllegalArgumentException,属性的getter方法,避免冗余数据 - IllegalArgumentException, getter method of property, avoiding redundant data Hibernate + Ehcache-发生IllegalArgumentException调用复合ID的getter - Hibernate + Ehcache - IllegalArgumentException occurred calling getter of composite id Bean类的无效属性&#39;id&#39;:Bean属性&#39;id&#39;不可读或具有无效的getter方法:getter的返回类型是否匹配 - Invalid property 'id' of bean class: Bean property 'id' is not readable or has an invalid getter method: Does the return type of the getter match 休眠“发生IllegalArgumentException调用getter” - hibernate “IllegalArgumentException occurred calling getter” Spring 将 Batch DB 引导至 CSV bean 的无效属性“ID” class Bean 属性“ID”不可读或具有无效的 getter 方法 - Spring Boot Batch DB to CSV Invalid property 'ID' of bean class Bean property 'ID' is not readable or has an invalid getter method Hibernate - 调用getter of model时发生了IllegalArgumentException - Hibernate - IllegalArgumentException occurred calling getter of model Hibernate Envers无法为可嵌入表“定位属性的获取方法” - Hibernate Envers is not able to “locate getter method for property” for Embeddable table 没有属性的吸气剂方法……错误 - No getter method for property… error 没有属性的吸气剂方法 - No getter method for property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM