# Problem mit Hibernate und Named Query



## marky8264 (24. Feb 2011)

hey,

ich möchte eine kleine Anwendung mit Spring und Hibernate schreiben. Dabei habe ich eine Named Query erstellt mit einem Parameter.
Hier die Entität:

```
@Entity
@Table(name="PersonVersion")
@SuppressWarnings("serial")
@NamedQueries({
	@NamedQuery(name="PersonVersion.findLastByPersonId"
		,query="from PersonVersion v where v.id = :Id")
})
public class PersonVersion implements Serializable{
}
```

Dabei bekomme ich folgende Exception:

```
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
	at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:286)
	at org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible(DefaultJpaDialect.java:120)
	at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
	at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
	at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:188)
	at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:146)
	at org.springframework.orm.jpa.JpaTemplate.findByNamedQuery(JpaTemplate.java:340)
	...
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
	at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:435)
	at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:71)
	at org.springframework.orm.jpa.JpaTemplate$11.doInJpa(JpaTemplate.java:346)
	at org.springframework.orm.jpa.JpaTemplate$11.doInJpa(JpaTemplate.java:1)
	at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:183)
	... 43 more
Caused by: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
	at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:81)
	at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:87)
	at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:445)
	at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:417)
	at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:429)
	... 47 more
```

Hier mein Aufruf:

```
List<PersonVersion> lst=(List<PersonVersion>)this.getJpaTemplate()
			.findByNamedQuery("PersonVersion.findLastByPersonId",id);
```

Ich hoffe ihr könnt mir weiter helfen.
mfg


----------



## marky8264 (24. Feb 2011)

Habe gerade heraus gefunden, dass wenn ich die findByNamedQuery-Methode verwende, dann kann ich keine named Parameter verwenden.
Um die named Parameter zu verwenden muss man findByNamedQueryAndNamedParams aufrufen.


----------

