I get the following Error when trying to select with across a many-to-many where the Primary keys are Composite keys made up of two Foreign keys.
Any ideas would be greatly appreciated! I've searched around and haven't found this model scenario produce this result. Thanks in advance!
Here's the error with mappings below.
The HQL is: "from ProductLang p WHERE (:category0 = some elements(p.categories)"
23281 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: S1009
23281 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Parameter index out of range (5 > number of parameters, which is 4).
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2231)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
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:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy57.findByCat(Unknown Source)
at com.products.TestProduct.findByCat(TestProduct.java:47)
at com.products.TestProduct.testSearch(TestProduct.java:134)
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:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
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:79)
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.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3288)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3272)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3314)
at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3258)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setInt(NewProxyPreparedStatement.java:677)
at org.hibernate.loader.Loader.bindLimitParameters(Loader.java:1667)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1592)
at org.hibernate.loader.Loader.doQuery(Loader.java:696)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
... 43 more
Code:
@Embeddable
public class ProductId implements Serializable, ILocaleId
{
private static final long serialVersionUID = 1L;
private Product entity;
private Locale locale;
public ProductId(){}
public ProductId(Product entity, Locale locale)
{
super();
this.entity = entity;
this.locale = locale;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ENTITY_ID")
public Product getEntity()
{
return entity;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="LOCALE_ID")
public Locale getLocale()
{
return locale;
}
public void setEntity(Product entity)
{
this.entity = entity;
}
public void setLocale(Locale locale)
{
this.locale = locale;
}
}
public class ProductLang implements ILocaleEntity
{
private ProductId localeId;
private Set<CategoryLang> categories;
@Id
@EmbeddedId
public ProductId getLocaleId()
{
return localeId;
}
public void setLocaleId(ProductId localeId)
{
this.localeId = localeId;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(
name="categorylang_productlang",
joinColumns={
@JoinColumn(name="PRODUCT_LANG_LOCALE_ID", referencedColumnName="LOCALE_ID"),
@JoinColumn(name="PRODUCT_LANG_ENTITY_ID", referencedColumnName="ENTITY_ID")
},
inverseJoinColumns={
@JoinColumn(name="CATEGORY_LOCALE_ID", referencedColumnName="LOCALE_ID"),
@JoinColumn(name="CATEGORY_ENTITY_ID", referencedColumnName="ENTITY_ID")
}
)
public Set<CategoryLang> getCategories()
{
return categories;
}
.......
}
and the other side....
Code:
@Embeddable
public class CategoryId implements Serializable, ILocaleId
{
private static final long serialVersionUID = 1L;
private Category entity;
private Locale locale;
public CategoryId(){}
public CategoryId(Category entity, Locale locale)
{
super();
this.entity = entity;
this.locale = locale;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ENTITY_ID")
public Category getEntity()
{
return entity;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="LOCALE_ID")
public Locale getLocale()
{
return locale;
}
@Override
public void setLocale(Locale locale)
{
this.locale = locale;
}
public void setEntity(Category entity)
{
this.entity = entity;
}
}
public class CategoryLang implements ILocaleEntity,Serializable
{
private CategoryId localeId;
@Id
public CategoryId getLocaleId()
{
return localeId;
}
}