I have a problem with an entity.
I'm modeling many pojos. I'm mapping them with JPA.
Before the problem I had a pojo (lets call it Pojo01). Pojo01 maps a relationship between Pojo02 and Pojo03 and adds one more property:
Code:
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = {
"POJO_02",
"POJO_03" }) })
public class Pojo01 implements Serializable {
private Long id;
private Pojo02 pojo02;
private Pojo03 pojo03;
private Boolean good;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "generator_pojo01")
@SequenceGenerator(name = "generator_pojo01", sequenceName = "SEQ_POJO_01")
@Column(name = "ID")
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
@ManyToOne(targetEntity = Pojo02.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name = "POJO_02", nullable = false, updatable = false)
public Pojo02 getPojo02() {
return pojo02;
}
public setPojo02(Pojo02 pojo02){
this.pojo02 = pojo02;
}
@ManyToOne(targetEntity = Pojo03.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name = "POJO_03", nullable = false, updatable = false)
public Pojo03 getPojo03() {
return pojo03;
}
public setPojo03(Pojo03 pojo03){
this.pojo03 = pojo03;
}
@Column(name = "GOOD")
public Boolean getGood() {
return good;
}
protected void setGood(Boolean good) {
this.good = good;
}
}
Pojo03 also has a realtionship with Pojo04:
Code:
@Entity
public class Pojo03 implements Serializable {
private Long id;
private Pojo04 pojo04;
/* It has more properties */
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "generator_pojo03")
@SequenceGenerator(name = "generator_pojo03", sequenceName = "SEQ_POJO_03")
@Column(name = "ID")
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
@ManyToOne(targetEntity = Usuario.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name = "POJO_04", nullable = false, updatable = false)
public Pojo04 getPojo04() {
return pojo04;
}
public setPojo04(Pojo04 pojo04){
this.pojo04 = pojo04;
}
}
I have tested the DAO layer and it works fine.
Now I tell my problem.
I create a new pojo (PojoX) it implements the relationship between Pojo01 and other one (Pojo05).
Code:
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = {
"POJO_01",
"POJO_05" }) })
public class PojoX implements Serializable {
private Long id;
private Pojo01 pojo01;
private Pojo05 pojo05;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "generator_pojox")
@SequenceGenerator(name = "generator_pojox", sequenceName = "SEQ_POJO_X")
@Column(name = "ID")
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
@ManyToOne(targetEntity = Pojo01.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name = "POJO_01", nullable = false, updatable = false)
public Pojo01 getPojo01() {
return pojo01;
}
public setPojo01(Pojo01 pojo01){
this.pojo01 = pojo01;
}
@ManyToOne(targetEntity = Pojo05.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name = "POJO_05", nullable = false, updatable = false)
public Pojo05 getPojo05() {
return pojo05;
}
public setPojo05(Pojo05 pojo05){
this.pojo05 = pojo05;
}
}
Well, when I test the DAO, when I do a DaoPojoX.save(pojox) it fails. The trace is:
Code:
-------------------------------------------------------------------------------
Test set: test.dao.hibernate.PojoXDaoTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.078 sec <<< FAILURE!
testAddAndRemovePojoX(test.dao.hibernate.PojoXDaoTest) Time elapsed: 0.062 sec <<< ERROR!
test.exception.MyOwnException: ERROR_GENERAL_BD
at test.exception.ExceptionUtil.createMyOwnException(ExceptionUtil.java:124)
at test.dao.hibernate.GenericDAOImpl.save(GenericDAOImpl.java:221)
at test.dao.hibernate.PojoXDaoTest.testAddAndRemovePojoX(PojoXDaoTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of test.model.Pojo03.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.model.Pojo03.id
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:824)
at test.dao.hibernate.GenericDAOImpl.save(GenericDAOImpl.java:218)
... 25 more
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.model.Pojo03.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3596)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:98)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:178)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
at org.springframework.orm.hibernate3.HibernateTemplate$23.doInHibernate(HibernateTemplate.java:827)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 28 more
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(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 47 more
IllegalArgumentException occurred calling getter of test.model.Pojo03.id ?!?!?!?!