我使用java hibernate
我有2个表,表1和表2有多对多的关系
表1:
@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 column1;
@Column()
String column2;
....
and getter and setter methods
}
表2:
@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
}
问题:
当我尝试从spring项目创建查询时,我收到此错误
错误:
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)
查询内部功能:
....
@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();
}
....
任何人都可以告诉我错误在哪里
注意:当我从此查询中删除节点时,不会发生错误。
答案 0 :(得分:0)
将查询更改为
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();
}