【问题标题】:Hibernate cannot resolve type with enumHibernate 无法使用枚举解析类型
【发布时间】:2013-06-23 15:25:15
【问题描述】:

我有两个实体,一个具有特定枚举的字段。现在我想检索所有具有特定枚举值的内容。

@Entity
@Table(name = "AlarmEvent")
@XmlRootElement
public class AlarmEvent implements Identifiable {
    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "guid")
    @Column(name = "Id", columnDefinition = "uniqueidentifier")
    private String id;

    @ManyToOne
    @JoinColumn(name = "AlarmType_Id")
    @XmlJavaTypeAdapter(EntityAdapter.class)
    private AlarmEventDefinition alarmType;

    @Column(name = "Timestamp", nullable = false)
    private Date timestamp;

    @Override
    @XmlTransient
    public String getIdentifier() {
        return id;
    }

    public AlarmEventDefinition getAlarmType() {
        return alarmType;
    }

    public void setAlarmType(AlarmEventDefinition alarmType) {
        this.alarmType = alarmType;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
}

和第二个实体

@Entity
@Table(name = "AlarmEventDefinition")
@XmlRootElement
public class AlarmEventDefinition implements Identifiable {
    public static enum AlarmEventDefinitionType {
        Start, End, Trigger, Report
    }

    @Id
    @Column(name = "Id")
    private Integer id;

    @Column(name = "Name_DE", length = 256)
    private String nameDe;

    @Column(name = "Type")
    @Enumerated(EnumType.STRING)
    private AlarmEventDefinitionType type;

    @OneToMany(mappedBy = "alarmType", fetch = FetchType.LAZY)
    @XmlTransient
    private List<AlarmEvent> events;

    public List<AlarmEvent> getEvents() {
        return events;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNameDe() {
        return nameDe;
    }

    public void setNameDe(String nameDe) {
        this.nameDe = nameDe;
    }

    public AlarmEventDefinitionType getType() {
        return type;
    }

    public void setType(AlarmEventDefinitionType type) {
        this.type = type;
    }
}

现在我想做这个

List<AlarmEvent> result = (List<AlarmEvent>) session.createCriteria(AlarmEvent.class)
        .add(Restrictions.eq("alarmType.type", AlarmEventDefinitionType.Trigger))
        .list();

要检索所有 AlarmEvents,它们的 AlarmEventDefinitions 类型是 Trigger。 休眠 说:

org.hibernate.QueryException:无法解析属性: alarmType.type of: datamodel.AlarmEvent

【问题讨论】:

    标签: java hibernate orm enums


    【解决方案1】:

    你只是在AlarmEvent 类中定义了一个alarmType 属性,没有alarmType.type。这就是错误消息的意思。

    您需要将条件扩展到关联的类AlarmEventDefinition。代码应该是这样的:

    List<AlarmEvent> result = (List<AlarmEvent>) session.createCriteria(AlarmEvent.class)
            .createCriteria("alarmType").add(Restrictions.eq("type", AlarmEventDefinitionType.Trigger))
            .list();
    

    【讨论】:

    • alarmType 是 AlarmEventDefinition 的实例。 AlarmEventDefinition 有一个字段类型(枚举),那么为什么不应该有一个alarmType.type? 导航:AlarmEvent --alarmType--> AlarmEventDefinition --type--> AlarmEventDefinitionType
    • 查看我的评论。属性类型属于 AlarmEventDefinition 类而不是 AlarmEventDefinitionType
    • @Coxer。是的,我一开始忽略了这一点,但更新后的答案应该可以解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 2015-09-19
    • 2012-08-05
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多