1
我使用Hibernate版本3.6。我必須在數據庫中存儲Class
實例(例如Integer.class
) 並且可能我可以這麼做 - NetBeans不會顯示錯誤,因爲在Object
的情況下。通過Hibernate在MySQL數據庫中存儲類對象
我Groups
表:
@Entity
public class Groups implements Serializable {
@Id
@GeneratedValue
private Long id;
private Class classOfGroup;
public void setClassOfGroup(Class classOfGroup) {
this.classOfGroup = classOfGroup;
}
public Class getClassOfGroup() {
return classOfGroup;
}
private void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
而且AdditionalInformationGroup
表:
@Entity
public class AdditionalInformationGroup implements Serializable {
@Id
@GeneratedValue
private Long id;
private Class groupClass;
private Boolean uniqueValue;
public void setUniqueValue(Boolean uniqueValue) {
this.uniqueValue = uniqueValue;
}
public Boolean getUniqueValue() {
return uniqueValue;
}
public void setGroupClass(Class groupClass) {
this.groupClass = groupClass;
}
public Class getGroupClass() {
return groupClass;
}
private void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
當我這樣做:
List<AdditionalInformationGroup> additionalInformationGroups =
s.createCriteria(AdditionalInformationGroup.class)
.add(Restrictions.eq("groupClass", group.getClassOfGroup())).list();
我得到異常:
Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: Unsupported discriminator type null
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getTypedValue(CriteriaQueryTranslator.java:499)
at org.hibernate.criterion.SimpleExpression.getTypedValues(SimpleExpression.java:71)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getQueryParameters(CriteriaQueryTranslator.java:251)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at Views.ConditionDialog.setFieldList(ConditionDialog.java:85)
我檢查過,在我的Groups
類的實例中沒有null。我有沒有bug,或者我不能通過hibernate存儲Class
的實例?
我不確定這個異常是由於你使用Class類型的字段造成的。但是,爲什麼不直接將類名存儲爲字符串呢? –
使我的代碼複雜一點,我想檢查所有可能的解決方案。 –