在Hibernate中删除一组日期

时间:2015-02-13 12:05:08

标签: java hibernate

我想在时间表List满足我的要求时从数据库中删除CinemaProgramme。当我试图删除cinemaProgramme时,我得到了一个例外,因为列表中的外键我无法删除它。 我听说过" OneToMany和OneToOne注释和XML上的orphanRemoval选项。孤立删除将确保从数据库中删除不再从关系中引用的任何对象。" 但我不能使用它,因为我有一个elementcollection映射而不是onetomany等。

所以我试图首先删除列表然后删除CinemaProgramme,但是当尝试执行时:

String hql1 = "delete CinemaProgramme.timetable as p where p in (:list)";
    Query q1 = session.createQuery(hql1);       
    q1.setParameterList("list", q.list());
    q1.executeUpdate();

我得到一个例外 org.hibernate.hql.ast.QuerySyntaxException:未映射CinemaProgramme.timetable。我在这个问题上花了好几个小时:(

CinemaProgramme类:

   @Entity
    @Table(name = "cinema_programme")
    public class CinemaProgramme implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private int id;

    @ElementCollection
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinColumn(name = "programme_id")
    private List<Date> timetable;

    @ManyToOne(cascade= CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "movie_id")
    private Movie movie;

    @Column(name = "dubbing")
    private boolean dubbing;
}

....... 编辑     为:

String hql1 = "delete CinemaProgramme as p where p.timetable in (:list)";
Query q1 = session.createQuery(hql1);
            List d = q.list();
            q1.setParameterList("list", d);
System.out.println(d); - displays [2015-02-01 00:00:00.0]

我得到: .....

7772 [http-bio-8080-exec-3] ERROR org.hibernate.util.JDBCExceptionReporter - No value specified for parameter 
dContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No value specified for parameter 1
    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.checkAllParametersSet(PreparedStatement.java:2513)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2489)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2336)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
    at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:101)
    ... 46 more

1 个答案:

答案 0 :(得分:2)

认为您的查询必须String hql1 = "delete CinemaProgramme as p where p.timetable in (:list)";