基本上我正在尝试实现一个Observer模式,Data被存储到数据库中,但是检索它时显示错误。 我想要创建的是一组类别,所有叶子节点都将包含问题。我正在学习休眠,所以任何类型的帮助都将受到赞赏。 错误如下。
错误:无法解析属性:parent_child_id:hierarchy.Component 在 org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)...
这是一个Component类
@Entity
@Table(name = "Component")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Component {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int component_id;
private String name;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_child_id")
private Component parent;
public Component() {
}
public Component(String name, Component parent) {
super();
this.name = name;
this.parent = parent;
}
public int getComponent_id() {
return component_id;
}
public void setComponent_id(int component_id) {
this.component_id = component_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*public abstract Iterator<Component> getChildren();
public abstract Component getParent();*/
public List<Component> getChildren() {
throw new UnsupportedOperationException();
}
public void setParent(Component parent) {
this.parent = parent;
}
public Component getParent() {
return this.parent;
}
public void setSize(int size) {
throw new UnsupportedOperationException();
}
public int getSize() {
throw new UnsupportedOperationException();
}
}
这是一个分类
@Entity
public class Category extends Component {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
private List<Component> children = new ArrayList<Component>(0);
public Category() {
}
public Category(String name, Component parent) {
super(name, parent);
}
@Override
public List<Component> getChildren() {
return children;
}
}
这是一个问题类
@Entity
public class Question extends Component {
private int size;
public Question() {
}
public Question(String name, Component parent) {
super(name, parent);
}
@Override
public void setSize(int size) {
this.size = size;
}
@Override
public int getSize() {
return this.size;
}
}
这是主类
public class Main {
private SessionFactory factory;
public Main() {
factory = HibieUtil.getSessionFactory();
}
public void insertData(Component root) {
Session sessie = factory.openSession();
Transaction transaction = null;
try {
transaction = sessie.beginTransaction();
sessie.save(root);
transaction.commit();
} catch(HibernateException e) {
if(transaction != null)
transaction.rollback();
e.printStackTrace();
} finally {
sessie.close();
}
}
public void showData(Component node) {
if(node == null) return;
System.out.println(node.getName());
Iterator<Component> children = node.getChildren().iterator();
while(children.hasNext())
showData(children.next());
}
public void display() {
Session sessie = factory.openSession();
Transaction transaction = null;
try {
transaction = sessie.beginTransaction();
Criteria criteria = sessie.createCriteria(Component.class);
criteria.add(Restrictions.eq("parent_child_id", null));
@SuppressWarnings("unchecked")
List<Component> result = criteria.list();
if(result != null)
showData(result.get(0));
transaction.commit();
} catch(HibernateException e) {
if(transaction != null)
transaction.rollback();
e.printStackTrace();
} finally {
sessie.close();
}
}
public static void main(String[] args) {
Main main = new Main();
Component root = new Category("Root", null);
Component childA = new Category("ChildA", root);
Component childB = new Category("ChildB", root);
root.getChildren().add(childA);
root.getChildren().add(childB);
Component questionA = new Question("Question A", childA);
questionA.setSize(10);
Component quesitonB = new Question("Question B", childA);
quesitonB.setSize(5);
childA.getChildren().add(questionA);
childA.getChildren().add(quesitonB);
main.insertData(root);
main.display();
}
}
答案 0 :(得分:1)
在您的显示方法中,您必须指定组件的属性名而不是数据库更改中的列名:
criteria.add(Restrictions.eq("parent_child_id", null));
由:
criteria.add(Restrictions.eq("parent", null));