-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate & Struts 2 - Many-to-One issue
PostPosted: Tue Mar 20, 2007 12:51 pm 
Newbie

Joined: Tue Mar 20, 2007 12:35 pm
Posts: 4
Hi,

I'm working with Struts 2 and Hibernate 3for about two weeks and it's my first experience with both. I'm having difficulty with 1:n relations.

I've got two tables: Company and Person.
Company has an owner, which is a reference to a specific Person.
I've implemented the mapping like this in Company.hbm.xml:

Code:
<many-to-one column="OWNER_ID" name="owner" class="model.person.Person"/>


The Company class has a getter and setter:
Code:
private Long id;
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}


The CompanyAction class used by Struts has:
Code:
private List persons;
private static PersonService personService = new PersonServiceHibernate();

public void prepare() throws Exception {
persons = personService.getAllPersons();
...
}


And the Company.jsp (which uses Struts 2) has:
Code:
<s:select label="%{getText('company.label.owner')}" name="company.owner" list="persons" listKey="id" listValue="firstName" emptyOption="true"/>



The JSP creates a nice choice-box (I would rather have an LOV pop-up with some search functionality, but that's for later). I can select the correct Person by name and the value is also present (checked in the html).

However, when I submit the form, I get the following error:
Code:
javax.servlet.ServletException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of model.person.Person.id
   org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:518)
   org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)

root cause

org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of model.person.Person.id
   org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
   org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
   org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3585)

etc.

If I submit without filling in an owner, all works fine. I guess, when any one of my companies has an owner, the error occurs. Can anyone help me out?

Tnx in advance
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 12:54 pm 
Newbie

Joined: Tue Mar 20, 2007 12:35 pm
Posts: 4
The record is not committed to the database, so my guess is wrong.
The error appears to occur, before any database transactions are made.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 1:03 pm 
Newbie

Joined: Tue Mar 20, 2007 12:35 pm
Posts: 4
I've logged some info on the record to be saved in the CompanyServiceHibernate class:

Code:
public void insertCompany(Company company) {
  System.out.println("Company:");
  System.out.println("Name: " + company.getName());
  System.out.println("Owner: " + company.getOwner());
  System.out.println("Id: " + company.getId());
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  session.save(company);
  session.getTransaction().commit();
}


Hibernate: select person0_.ID as ID0_, pers
0_.lastName as lastName0_, person0_.usernam
as COMPANY5_0_ from PERSON person0_
Company:
Name: Some other company
Owner: 1
Id: null
Hibernate: select max(ID) from COMPANY



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.