1

大家好,当我尝试执行我的代码时,我遇到了一个异常:另外请告诉我如何传递给参数以进行查询或使用

我的道:

@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 ;

        }

}

请帮我解决它。

4

1 回答 1

0

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

最后它看起来像这样:

select i from Institution i JOIN i.creationDate d where i.nameOfInstitution =? and d.particularDate=?
于 2013-05-23T13:31:48.907 回答