hibernate子表记录没有被删除

时间:2016-07-16 10:01:32

标签: hibernate hibernate-cascade javax.persistence

我有两个父子关系表如下:

 Parent Table:
@Entity
@Table(name = "frontend.rdp_strategy", uniqueConstraints = {@UniqueConstraint(columnNames = {"name", "datatype","source"})})
public class FrontRdpStrategy implements java.io.Serializable {

    @Id 
//  @GeneratedValue(generator="optimized-sequence")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private String datatype;
    private String source;
    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER,  mappedBy="frontRdpStrategy") 
    private Set<RdpStrategySequence> rdpStrategySequences = new HashSet<RdpStrategySequence>();

Child Table:
@Entity
@SuppressWarnings("serial")
@Table(name = "frontend.rdp_strategy_sequence")
public class RdpStrategySequence implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
//  @GeneratedValue(generator="optimized-sequence")
    private long id;
    @ManyToOne(cascade = {CascadeType.ALL},fetch=FetchType.EAGER)
    @JoinColumn(name = "rdp_strategy_id", nullable = false)
    private FrontRdpStrategy frontRdpStrategy;
    @ManyToOne( fetch=FetchType.EAGER)
    @JoinColumn(name = "rdp_strategy_condition_id", nullable = false)
    private RdpStrategyCondition rdpStrategyCondition;

这里CascadeType是javax.persistence.CascadeType。现在我尝试使用以下查询删除子表中特定父表id的子表记录:

 session.createQuery("delete from frontend.rdp_strategy_sequence m where m.rdp_strategy_id = :id").setLong("id", id).executeUpdate();

但我收到以下异常:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: frontend.rdp_strategy_sequence is not mapped [delete from frontend.rdp_strategy_sequence m where m.rdp_strategy_id = :id]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
    at frontend.guifx.dao.frontend.FrontendGUIFxDao.deleteSequenceForRdp(FrontendGUIFxDao.java:111)
    at frontend.guifx.controller.EditRdpStrategyController.saveRdpStrategy(EditRdpStrategyController.java:637)
    ... 60 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: frontend.rdp_strategy_sequence is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.deleteStatement(HqlSqlBaseWalker.java:435)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:263)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 69 more

Kinldy同样建议,我做错了吗?

1 个答案:

答案 0 :(得分:1)

你必须使用实体名称,如下所示:

session.createQuery("delete from RdpStrategySequence m where m.id = :id").setLong("id", id).executeUpdate();