user5714309
user5714309

Reputation:

Hibernate Inheritance mapping

Basically I am trying to implement an Observer pattern, Data is getting stored into the database but the while retrieving it is showing an error. What I am trying to create is a set of categories and all the leaf nodes will contain questions.I am learning hibernate, so any kind of help will be appreciated. Error is given below.

ERROR: could not resolve property: parent_child_id of: hierarchy.Component at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83) ...

This is a Component class

@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();
}
}

This is a Category class

@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;
}   
}

This is a Question class

@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;
}
}

And this is the Main class

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();
}

}

Upvotes: 0

Views: 93

Answers (1)

Abass A
Abass A

Reputation: 743

In your display method you must specify propertyname of component not the column name in database change :

 criteria.add(Restrictions.eq("parent_child_id", null));

by:

 criteria.add(Restrictions.eq("parent", null));

Upvotes: 1

Related Questions