I'm a newbie, and I'm trying to map a simple nonidentifying many-to-one relationship betwean simple two relations. I have two relations in the MySQL database:
Person:
- person_id (PK), INTEGER, AUTO_INCREMENT, NOT NULL
- person_firstName, VARCHAR(45)
- person_secondName, VARCHAR(45)
News:
- news_id (PK), INTEGER, AUTO_INCREMENT, NOT NULL
- person_id (FK), INTEGER, NOT NULL references Person(person_id)
Other relevant information is shown below. I'm trying to insert a simple news entity in News relation using Hibernate, but it throws exception. There are several Person entities in Person relation already, with primary key values 1,2,3,... I already tried to find solution using google, this forum, Hibernate documentation etc. But, didn't find anything useful regarding this problem.
Hibernate version: 3.3.1
Mapping documents:
Person.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="database.Person" table="Person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="firstName" column="person_firstName" />
<property name="secondName" column="person_secondName" />
</class>
</hibernate-mapping>
News.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="database.News" table="News">
<id name="id" column="news_id">
<generator class="native"/>
</id>
<many-to-one name="person" class="database.Person" column="person_id" not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
session2.beginTransaction();
News news = new News();
news.setPerson(2);
session2.save(news);
session2.getTransaction().commit();
Full stack trace of any exception that occurs:Code:
2922 [http-8080-2] ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: database.Person, getter method of property: id
2008-11-08 17:02:14 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet MyServlet threw exception
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of database.Person.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:206)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3619)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3335)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:204)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:160)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:92)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:70)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:311)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
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.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy0.save(Unknown Source)
at MyServlet.doGet(MyServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
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:169)
... 39 more
Persistent classes:Person.java
Code:
package database;
public class Person {
private int id;
private String firsName;
private String secondName;
public Person() {}
public int getId() {
return this.id;
}
@SuppressWarnings("unused")
private void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName= firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
}
News.java
Code:
package database;
public class News {
private int id;
private int person;
public News () {}
public int getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(int id) {
this.id = id;
}
public int getPerson() {
return person;
}
public void setPerson(int person) {
this.person = person;
}
}
Name and version of the database you are using: MySQL Community Server 5.0.67
The generated SQL (show_sql=true): There is no generated SQL statements on the console, because of the exception.