代码之家  ›  专栏  ›  技术社区  ›  Jim Barrows

在没有名为“type”的属性的类中,我得到org.hibernate.propertynotfoundexception:在类中找不到属性类型的setter

  •  0
  • Jim Barrows  · 技术社区  · 15 年前

    使用JPA和Hibernate作为提供程序,我有一个类定义如下:

    @Entity
    @Table(name="partyrole")
    public class PartyRole extends BaseDateRangeModel {
      private static final long serialVersionUID = 1L;
      private Party roleFor;
    
      public void setRoleFor(Party roleFor) {
        this.roleFor = roleFor;
      }
    
      @ManyToOne
      public Party getRoleFor() {
        return roleFor;
      }
    }
    

    我得到了问题标题上的错误。我试着补充 public void setType(Object type) 但这也不管用。persistence.xml文件是正常的。 有两个类引用这个类,但它们都不尝试调用 setType 要么。我会抓住任何帮助的!!!!! 这发生在部署时。堆栈跟踪位于底部。

    父类:

    package com.nsfw.bmp.common.jpa;
    
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.MappedSuperclass;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.persistence.Transient;
    
    import org.hibernate.validator.AssertTrue;
    import org.hibernate.validator.NotNull;
    
    /**
     * Several models are date range sensitive, this base class provides that basic
     * functionality.
     * 
     * @author jim
     * 
     */
    @MappedSuperclass
    public abstract class BaseDateRangeModel extends BasePersistentModel {
    private static final long serialVersionUID = 1L;
    private Date from;
    private Date thru;
    
    /**
     * Determines if a model is active. A model is active if now is after or
     * equal to from , and thru is either null, or after now, or equal to now.
     */
    @Transient
    public boolean isActive() {
        Date now = new Date();
        boolean afterFrom = from.before(now) || from.equals(now);
        boolean beforeThru = thru == null || thru.after(now)
                || thru.equals(now);
        return afterFrom && beforeThru;
    }
    
    @AssertTrue(message = "Dates are not valid the thru date must be empty, or after the fromdate.")
    public boolean areDatesValid() {
        if (thru == null) {
            return true;
        } else {
            return thru.after(from);
        }
    }
    
    
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    @Column(name = "fromDate")
    public Date getFrom() {
        return from;
    }
    
    public void setFrom(Date from) {
        this.from = from;
    }
    
    @Temporal(TemporalType.TIMESTAMP)
    public Date getThru() {
        return thru;
    }
    
    public void setThru(Date thru) {
        this.thru = thru;
    }
    

    }

    它的父母:

    package com.nsfw.bmp.common.jpa;
    
    import java.io.Serializable;
    
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.MappedSuperclass;
    import javax.persistence.Version;
    
    @MappedSuperclass
    public abstract class BasePersistentModel implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    protected Long id;
    
    protected Long version = 0l;
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BasePersistentModel other = (BasePersistentModel) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (version == null) {
            if (other.version != null)
                return false;
        } else if (!version.equals(other.version))
            return false;
        return true;
    }
    
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    
    @Version
    public Long getVersion() {
        return version;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((version == null) ? 0 : version.hashCode());
        return result;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public void setVersion(Long version) {
        this.version = version;
    }
    

    }

    Party类相当大,有很多映射。这是要求的:

    /**
     * @return the actingAs
     */
    @OneToMany(mappedBy="roleFor", targetEntity=com.nsfw.bmp.party.entity.association.PartyRole.class)
    @OrderBy("from")
    public List<PartyRole> getActingAs() {
        return actingAs;
    }
    

    这是堆栈跟踪:

    Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property type in class com.nsfw.bmp.party.entity.association.PartyRole
    at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:240)
    at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:233)
    at org.hibernate.mapping.Property.getSetter(Property.java:299)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertySetter(PojoEntityTuplizer.java:272)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:149)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:76)
    at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:325)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:457)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:261)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    
    3 回复  |  直到 15 年前
        1
  •  0
  •   Gren    15 年前

    你能把你的派对课贴出来吗?我怀疑这与你的manytoone地图有关。partyrole表中有类型列吗?

        2
  •  0
  •   Luzifer42    15 年前

    您的问题可能链接到BaseDateRangeModel类,因为PartyRole扩展了它。 你能给我们看看那个班吗?

        3
  •  0
  •   ChssPly76    15 年前

    如果您在启动过程中得到这个,这意味着您在某个地方有一个通过反向关系引用partyrole的类,例如沿着

    @OneToMany(targetEntity=PartyRole.class, inverse=true")
    

    在其他实体中。将休眠日志记录级别设置为 DEBUG -它应该能帮助你缩小问题的范围。