I'm using Hibernate 3.2.6.GA as JPA implementation on Oracle 10g.
I have entity:
Code:
@MappedSuperclass
public abstract class KeyedItem implements Keyable<Long> {
@Id
@Column(name = "KLUCZ", unique = true, nullable = false, precision = 9, scale = 0)
private Long key;
public Long getKey() {
return key;
}
public void setKey(Long key) {
this.key = key;
}
}
@Entity
@Table(name = "ZESTAW_DANYCH")
public class DataSet extends KeyedItem {
//the rest of the code of this class is not required - it just extends KeyedItem
}
@SqlResultSetMappings(value={
@SqlResultSetMapping(name = "Attachment.ResultSetMapping", entities = @EntityResult(entityClass = Attachment.class, fields = {
@FieldResult(name = "key", column = "KLUCZ"),
@FieldResult(name = "name", column = "NAZWA"),
@FieldResult(name = "description", column = "OPIS"),
@FieldResult(name = "dataSet.key", column = "ZEDA_KLUCZ"),
}))
})
@NamedNativeQueries(value={
@NamedNativeQuery(name = "Attachment.Loader", resultSetMapping = "Attachment.ResultSetMapping", query = "SELECT KLUCZ, NAZWA, OPIS, ZEDA_KLUCZ from ZALACZNIK WHERE KLUCZ = ?"),
})
@Entity
@Table(name = "ZALACZNIK")
@SQLDelete(callable=true, sql="{call ZALACZNIK_UTILS.USUN_ZALACZNIK(?)}")
@Loader(namedQuery="Attachment.Loader")
public class Attachment extends KeyedItem {
private static final long serialVersionUID = -8762317560896428252L;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ZEDA_KLUCZ")
private DataSet dataSet;
@Column(name = "NAZWA", nullable = false, length = 255)
private String name;
@Column(name = "OPIS", length = 255)
private String description;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ZALACZNIKI_PROCEDURY_TESTOWEJ", schema = "PENTATR", joinColumns = { @JoinColumn(name = "ZALA_KLUCZ", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "PTST_KLUCZ", nullable = false, updatable = false) })
private Set<ScenarioTestProcedure> scenarioTestProcedures = new HashSet<ScenarioTestProcedure>(
0);
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ZALACZNIKI_PROCEDURY_PROW_TEST", schema = "PENTATR", joinColumns = { @JoinColumn(name = "ZALA_KLUCZ", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "PRPT_KLUCZ", nullable = false, updatable = false) })
private Set<TestAreaTestingProcedure> testingProcedures = new HashSet<TestAreaTestingProcedure>(
0);
public Attachment() {
}
public Attachment(Long key, String name) {
setKey(key);
this.name = name;
}
public Attachment(Long key, DataSet dataSet, String name,
String description,
Set<ScenarioTestProcedure> scenarioTestProcedures,
Set<TestAreaTestingProcedure> testingProcedures) {
setKey(key);
this.dataSet = dataSet;
this.name = name;
this.description = description;
this.scenarioTestProcedures = scenarioTestProcedures;
this.testingProcedures = testingProcedures;
}
public DataSet getDataSet() {
return this.dataSet;
}
public void setDataSet(DataSet dataSet) {
this.dataSet = dataSet;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<ScenarioTestProcedure> getScenarioTestProcedures() {
return this.scenarioTestProcedures;
}
public void setScenarioTestProcedures(Set<ScenarioTestProcedure> scenarioTestProcedures) {
this.scenarioTestProcedures = scenarioTestProcedures;
}
public Set<TestAreaTestingProcedure> getTestingProcedures() {
return this.testingProcedures;
}
public void setTestingProcedures(
Set<TestAreaTestingProcedure> testingProcedures) {
this.testingProcedures = testingProcedures;
}
}
and I want to use custom sql for loading it from database.
I found this article
http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#entity-mapping-query-native and definied SqlResultSetMapping and NamedNativeQueries as you can see it at listing. When I run this code I got this exception:
Code:
org.hibernate.MappingException: dotted notation reference neither a component nor a many/one to one
at org.hibernate.cfg.annotations.ResultsetMappingSecondPass.getSubPropertyIterator(ResultsetMappingSecondPass.java:210)
at org.hibernate.cfg.annotations.ResultsetMappingSecondPass.doSecondPass(ResultsetMappingSecondPass.java:86)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1145)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:654)
at com.bm.cfg.Ejb3UnitCfg.getEntityManagerFactory(Ejb3UnitCfg.java:161)
at com.bm.creators.SessionBeanFactory.getEntityManager(SessionBeanFactory.java:151)
at com.bm.creators.SessionBeanFactory.getInjector(SessionBeanFactory.java:104)
at com.bm.creators.SessionBeanFactory.createSessionBean(SessionBeanFactory.java:85)
at com.bm.testsuite.BaseSessionBeanFixture.setUp(BaseSessionBeanFixture.java:114)
at org.jmock.core.VerifyingTestCase.runBare(Unknown Source)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.ClassCastException: org.hibernate.mapping.SimpleValue
at org.hibernate.cfg.annotations.ResultsetMappingSecondPass.getSubPropertyIterator(ResultsetMappingSecondPass.java:202)
... 27 more
Why it is not working? In article there is exactly the same relation ManyToOne betweend SpaceShip and Captain?
If I remove FieldResult(name = "dataSet.key", column = "ZEDA_KLUCZ") from SqlResultSetMapping I got this exception:
Code:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:202)
at com.pentacomp.ptr.report.ejb.ReportsBean.findAttachment(ReportsBean.java:197)
at com.pentacomp.ptr.report.ejb.ReportBeanTest.testCreatePlanningReportDataForPlanVersion(ReportBeanTest.java:127)
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 org.jmock.core.VerifyingTestCase.runBare(Unknown Source)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2216)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at org.hibernate.persister.entity.NamedQueryLoader.load(NamedQueryLoader.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3049)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:399)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
... 21 more
Caused by: java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3291)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1914)
at oracle.jdbc.driver.OracleResultSet.getLong(OracleResultSet.java:1575)
at org.hibernate.type.LongType.get(LongType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:103)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2101)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
... 38 more
And in log file I can see that this line:
Code:
INFO LongType:182 - could not read column value from result set: ZEDA4_2_0_; Invalid column name
but there is no any ZEDA4_2_0_ column in my custom sql.
What I'm doing wrong?
Thanks in advice,
Marcin