File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Error with Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Error with Hibernate" Watch "Error with Hibernate" New topic
Author

Error with Hibernate

rodrigo bengoechea
Greenhorn

Joined: Nov 15, 2008
Posts: 10
Hi!, I`ve just start to develop using hibernate and I'm dealing with a one-to-many mapping using a List. Everything goes fine until the application tries to save the objects into the data base. I've been trying to figure out where's my mistake but I couldn't. If someone has any idea it would be great.

Above I post my two classes, the stacktrace and the hibernate config files.

Here's my class user:

package beans;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

public class User {

private long id;
private String name;
private String sirName;
private String user;
private List passwords;
private String authentication;
private Date registrationDate;
private String email;

public User(){
passwords = new ArrayList();
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSirName() {
return sirName;
}

public void setSirName(String sirname) {
this.sirName = sirname;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getAuthentication() {
return authentication;
}

public void setAuthentication(String authentication) {
this.authentication = authentication;
}

public Date getRegistrationDate() {
return registrationDate;
}

public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
}

public List getPasswords() {
return passwords;
}

public void setPasswords(List passwords) {
this.passwords = passwords;
}

public void setPassword(String password){
passwords.add(password);
}

public boolean hasPassword(String password){
return passwords.contains(password);
}
}

Here's the Password class

package beans;

public class Password {

private String value;
private long id;

/*public Password(String value){
this.value = value;
}*/

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}

The User.hbm file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="beans.Password" table="Password">
<id name="id" type="long" column="id" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="value" type="string" column="value"/>
</class>

<class name="beans.User" table="User">
<id name="id" type="long" column="id" unsaved-value="0">
<generator class="increment"/>
</id>
<list name="passwords" cascade="all">
<key column="user_id"/>
<index column="id"/>
<one-to-many class="beans.Password"/>
</list>
<property name="name" type="string" column="name"/>
<property name="sirName" type="string" column="sir_name"/>
<property name="user" type="string" column="user"/>
<property name="authentication" type="string" column="authentication"/>
<property name="email" type="string" column="email"/>
<property name="registrationDate" type="date" column="registration_date"/>
</class>
</hibernate-mapping>

Here the hibernate config file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- <property name="hibernate.c3p0.max_size">3</property>-->
<!--<property name="hibernate.c3p0.min_size">1</property>-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/gruposweb</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>

<mapping resource="beans/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

And finally the stacktrace

8:58,655 INFO [STDOUT] 19:38:58,655 ERROR [BasicPropertyAccessor] IllegalArgumentException in class: beans.Password, getter method of property: id
19:38:58,655 ERROR [STDERR] org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of beans.Password.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3257)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:2983)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:460)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:410)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:299)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at dao.UserDAOImpl.registerUser(UserDAOImpl.java:37)
at services.ServiceImpl.registerUser(ServiceImpl.java:15)
at action.register.RegisterAction.execute(RegisterAction.java:48)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)

Please if anyone has some idea please share it with me.

Regards!!!

Rodrigo.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109



Try removing the index column =... element from the list declaration and see if it works. The index column is used for sorting and i am taking a wild guess that maybe it's causing some issue.



If that does not work, let us know. Also please post the code where you are creating and saving these objects.


[My Blog] [JavaRanch Journal]
 
 
subject: Error with Hibernate
 
Similar Threads
one to many bidirectional error driving me mad
Many to Many Relation Error Driving Me Mad
one to many relation error
Mapping Error Hibernate
One to One not cascading on insert - IdentifierGenerationException