Hi guys,
I managed (as you can see in older posts from me) to insert a onetomany relation through hibernate. My two entity classes look like following:
Project.java:
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy="project")
@OrderColumn(name = "project_index")
List<Application> applications;
....
Application.java (which is a child of project. One project can have many applications, but one application belongs to just one project)
@Entity
public class Application {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
...
So far, insertion of data works well. But getting data out of my database is the problem. I tried two ways:
Way 1: I retrieve a Project and try to get Applications out of the list attribute. But unfortunately Application entities are in a 'storedSnapshot', which seems pretty wrong to me. Here is a screenshot from my debug screen:
Actually that way works! I did some mistakes somewhere else...
Way 2: I try to retrieve a list of all applications via sql query:
public List<Application> getApplications(int project_id) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Application> applications = session.createQuery("from Application a where a.project_id=" + project_id + " ").list();
return applications;
}
..which throws strange exception -.-
org.hibernate.QueryException: could not resolve property: project_id of: de..common.entities.Application [from de..common.entities.Application a where a.project_id=1 ]
Problem with way 2 is that that I mix up SQL and HQL.
A little help for a hibernate beginner would be great :-) Cheers..