I have the concept of a Company. A Company can have 0 to * Audits.
Here is Company:
Code:
public class Company implements Serializable {
...
@OneToMany(mappedBy="company", cascade=CascadeType.ALL)
@JoinColumn (name="COMPANY_ID")
private List<Audit> audits = new ArrayList<Audit>();
...
Here is Audit:
Code:
public class Audit implements Serializable {
...
@ManyToOne
private Company company;
...
I can persist a Company fine and load it like this:
Code:
Company cLoad = companyDao.findByName(TEST_COMPANY);
FindByName looks like this (class extends HibernateDaoSupport):
Code:
public Company findByName(final String name) {
return (Company) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = getSession().getNamedQuery("CompanyByName");
query.setString("name", name);
return (Company) query.uniqueResult();
}
});
}
I then build a Audit and Associate it like this:
Code:
audit = new Audit();
audit.setCompany(cLoad);
audit.setAuditDate(new Date());
audit.setAreas(areas);
audit.setMaterials(materials);
audit.setCategories(categories);
auditDao.save(audit);
List<Audit> audits = new ArrayList<Audit>();
audits.add(audit);
cLoad.setAudits(audits);
companyDao.merge(cLoad);
Merge just calls this (class extends HibernateDaoSupport):
Code:
getHibernateTemplate().merge(company);
I get this exception:
Code:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [com.model.Company.audits#7]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.lukeshannon.immacutec.model.Company.audits#7]
Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.model.Company.audits#7]
.....
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'audits0_.AUDIT_ID' in 'field list'
I am still getting my head around Hibernate (how to configure my classes and then how to use the framework). Can anyone give a hint as to what I am doing wrong? I want to be able to create a Company and then add Audits to it. When I delete the Company I want to delete the Audits.
Thanks!