开发者

(No value specified for parameter 4) many to many relationShip error

i use java hibernate

i have 2 Tables Table1 and Table2 with many to many relationship

Table1:

@Entity
@Table(name="table1")
public class Table1 implements Serializable {
....
@ManyToMany(mappedBy="table1")
@JoinTable(
    name="table1_table2",
    joinColumns={@JoinColumn(name="table2_id")},
    inverseJoinColumns={@JoinColumn(name="table1_id")})
Set<Table2>table2;

@Column()
String colu开发者_C百科mn1;

@Column()
String column2;
....
and getter and setter methods
}

Table2:

@Entity
@Table(name="table2")
public class Table2  implements Serializable{
....
@ManyToMany(targetEntity=Table1.class,cascade = { CascadeType.ALL })
@Column(name="table1_id")
Set<Table1>table1;
....
and getter and setter methods
} 

Problem:

when i try to create a query from spring project, i get this error

error:

No value specified for parameter 4.
exception thrown < Table1.getByTable2AndColumn1(..) > exception message could not execute query with params [Table2 [....],..., ... ]   
at org.hibernate.exception.DataException: could not execute query           
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)
.....
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 4.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:146)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:183)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)

query inside function:

....
@Autowired
SessionFactory sessionFactory;

    .....
public Table1 getByTable2AndColumn1(Table2 table2, String column1, String column2) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Table1.class);

    if (table2 == null){
        criteria.add(Restrictions.isNull("table2"));
    }else{
        criteria.add(Restrictions.eq("table2", table2));
    }

    if (column1 == null){
        criteria.add(Restrictions.or(Restrictions.isNull("column1"), Restrictions.eq("column1", "")));
    }else{
        criteria.add(Restrictions.eq("column1", column1));
    }

    if (column2==null){
        criteria.add(Restrictions.isNull("column2"));
    }else{
        criteria.add(Restrictions.eq("column2", column2));
    }

    criteria.setMaxResults(1);


    return (Table1) criteria.uniqueResult();
}
 ....

can any one tell me where is the error

Note: when i remove node from this query no error occurs.


change query to

public Table1 getByTable2AndColumn1(Table2 table2, 
                                     String column1, String column2) {

Criteria criteria = sessionFactory.getCurrentSession()
                                  .createCriteria(Table1.class);

if (table2 == null){
    return null;
}

if (column1 == null){
    criteria.add(Restrictions.or(Restrictions.isNull("column1"), 
                                               Restrictions.eq("column1", "")));
}else{
    criteria.add(Restrictions.eq("column1", column1));
}

if (column2==null){
    criteria.add(Restrictions.isNull("column2"));
}else{
    criteria.add(Restrictions.eq("column2", column2));
}

Criteria c = criteria.createCriteria("table2");
c.add(Restrictions.eq("id", table2.getId()));
c.setMaxResults(1);

return (Table1) c.uniqueResult();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜