-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Adding a collectiong to an existing object issue
PostPosted: Fri May 01, 2009 5:25 pm 
Newbie

Joined: Fri Apr 10, 2009 3:53 pm
Posts: 6
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!


Top
 Profile  
 
 Post subject: Re: Adding a collectiong to an existing object issue
PostPosted: Fri May 01, 2009 5:29 pm 
Beginner
Beginner

Joined: Mon Jun 19, 2006 4:10 pm
Posts: 27
look at the unknown column part of the error. looks like you have the audit id mapped incorrectly (or the column on the table isn't being created with the name you think.)


Top
 Profile  
 
 Post subject: Re: Adding a collectiong to an existing object issue
PostPosted: Fri May 01, 2009 6:20 pm 
Newbie

Joined: Fri Apr 10, 2009 3:53 pm
Posts: 6
I saw that and am not sure what to make of it. I also have a one to Many relationship between Address and Company. It is mapped the same way and works fine.

It seems if I add the Audit when I create the Company for the first time all is well.

The issues come when I try and load it from the DB and then update it.

As a side note when I try to use the HibernateTemplate's 'save' method rather than 'merge' I get this.

Code:
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Caused by: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions


I feel like I have something misconfigured or I am not persisting things correctly in my test class.


Top
 Profile  
 
 Post subject: Re: Adding a collectiong to an existing object issue
PostPosted: Fri May 01, 2009 7:51 pm 
Beginner
Beginner

Joined: Mon Jun 19, 2006 4:10 pm
Posts: 27
this second issue is completely different and makes complete sense.

save is only for saving transient (i.e. entities that have not been saved) instances of things.

update is only for saving a detached instance (entity was loaded from persistent state and then it was de-associated with a session either programmatically or because the session closed).

merge is for taking the state of an object in unknown persistent state and making the persistent entity match.

the way i do my updating is

T entity = dao.loadById(id)
entity.set(values)

that is it. notice no update call. when the session closes it automatically flushes the state of the entity.

so the moral of all this is go back to merge or update.

And your first problem, i still insist that the column on the table is not what you mapped the key to.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.