预期位置参数计数:2,实际参数:

时间:2013-05-23 13:11:50

标签: java hibernate

大家好,我尝试执行代码时遇到异常: 另请告诉我如何传递参数来查询或使用

我的道:

@Override
    public Institution findWIthTwoParam(String name, Date mydate) {         
      return (Institution)sessionFactory.getCurrentSession().createQuery(
         "select i from Institution i where i.nameOfInstitution =? and i.creationDate=?")
         .setString(0, name)
         .setParameter(1, mydate).uniqueResult();

    }

我的异常堆栈:

Exception in thread "main" org.hibernate.QueryException: Expected positional parameter count: 2, actual parameters: [MyIstitution, Tue Apr 27 00:00:00 EST 17] [select i from Institution i where i.nameOfInstitution =? and i.creationDate=?]
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:366)
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:322)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:98)
    at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
    at edu.demidov.dao.InsertDataService.findWIthTwoParam(InsertDataService.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy25.findWIthTwoParam(Unknown Source)
    at edu.demidov.dao.AppTEst.main(AppTEst.java:76)

我的实体:

@Entity
@Table(name="INSTITUTION")
public class Institution implements Serializable{


    private static final long serialVersionUID = -7636394097858726922L;

            @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="INSTITUTION_ID")
    private int institutionId;


        @Version
        @Column(name="VERSION")
    private int version;


            @Column(name="NAME_INSTITUTION")
    private String nameOfInstitution;


                 @Column(name="TYPE_NAME")
    private String typeName;


        @ManyToMany(mappedBy="institution", fetch = FetchType.LAZY)
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
    private Set<CreationDate> creationDate = new HashSet<CreationDate>();

        //getter and setter methods ommited

        public String toString() {
            return institutionId + " , " + nameOfInstitution + " , " + typeName 
        }       
}

我的实体二:

@Entity
@Table(name="CREATION_DATE")
public class CreationDate implements Serializable {


    private static final long serialVersionUID = 1648102358397071136L;

                 @Id
        @GeneratedValue(strategy=IDENTITY)
        @Column(name="DATE_ID")
    private int dateId;


        @Column(name="PARTICULAR_DATE")
    private Date particularDate;


            @Version
        @Column(name="VERSION")
    private int version;


        @Temporal(TemporalType.DATE)
        @Column(name="CHILD_GO_SCHOOL_DATE")
    private Date childGoSchoolDate;


        @Temporal(TemporalType.DATE)
        @Column(name="CHILD_ADMISSION_DATE")
    private Date childAdmissionDate;


        @ManyToMany(fetch = FetchType.LAZY)
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
        @JoinTable(name="CREATIONDATE_INSTITUTION", 
                                    joinColumns=@JoinColumn(name="DATE_ID"), 
                                    inverseJoinColumns=@JoinColumn(name="INSTITUTION_ID"))
    private Set<Institution> institution = new HashSet<Institution>();


                      @OneToMany(fetch=FetchType.EAGER, orphanRemoval=true)
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
        @JoinTable(name="SRC_DATE", joinColumns=@JoinColumn(name="DATE_ID"),
                    inverseJoinColumns=@JoinColumn(name="SRC_ID"))
    private List<ScheduleRotationChild> scheduleRotationChild = new ArrayList<ScheduleRotationChild>();


        //getter and setter methods ommited


        public String toString() {

            return  dateId + " , " 
            + particularDate + " , " 
            + childGoSchoolDate + " , " 
            + childAdmissionDate + "  " + scheduleRotationChild ;

        }

}

请帮我解决一下。

1 个答案:

答案 0 :(得分:0)

您的creationDate是一套,所以您需要加入。该集本身包含CreationDate的实例,而不是日期。

最后看起来像这样:

select i from Institution i JOIN i.creationDate d where i.nameOfInstitution =? and d.particularDate=?