org.hibernate.hql.internal.ast.ErrorCounter - 第1行:199:意外令牌:加入

时间:2015-10-13 20:44:22

标签: java mysql hibernate spring-data-jpa

我有两个实体

@Entity
public class Video {    

    @Id
    @GeneratedValue
    private Integer id;     
    @Size(min = 5,max = 50, message = "Valid title is 5-50 chars")
    private String title;   
@ManyToMany(cascade = {CascadeType.MERGE})
@JoinTable(name = "vids_customers",
            joinColumns = @JoinColumn(name = "vids_id"),
            inverseJoinColumns = @JoinColumn(name = "customers_id"))
private List<Customer> customers;
// getters and setters goes here
}

    @Entity
    public class Customer {    

        @Id
        @GeneratedValue
        private Integer id;

        @Size(min = 5,max = 15, message = "Valid name is 5-15 chars")
        private String name;

        @ManyToMany(mappedBy = "customers", cascade = {CascadeType.MERGE})  
        private List<Videos> videos;
        // getters and setters goes here
}

我创建了一条记录

  

视频标题:为冠军站起来   客户1-John,2-James

我想更新视频记录,并希望添加或删除客户

  

视频标题:站起来......   客户1-John,2-James,3-Ria

所以我有一个查询将视频更新为

@Modifying
    @Query("UPDATE Video v SET v.title = :title WHERE v.id = :id")
    @Transactional
    void update(@Param("id") Integer id, @Param("title") String title);

上面的查询更新了视频记录,但没有和/或删除了客户,我想通过在上面的查询中进行更改来完成这项工作

@Modifying
    @Query("UPDATE Video v SET v.title = :title JOIN v.customers c WHERE v.id = :id")
    @Transactional
    void update(@Param("id") Integer id, @Param("title") String title);

我得到了

ERROR: org.hibernate.hql.internal.ast.ErrorCounter - line 1:199: unexpected token: JOIN
ERROR: org.hibernate.hql.internal.ast.ErrorCounter - line 1:199: unexpected token: JOIN
line 1:199: unexpected token: JOIN
    at org.hibernate.hql.internal.antlr.HqlBaseParser.updateStatement(HqlBaseParser.java:254)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:162)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
    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 org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
    at com.sun.proxy.$Proxy48.createQuery(Unknown Source)

如何编写查询以便我可以更新视频记录并希望添加或删除客户。

1 个答案:

答案 0 :(得分:1)

您无法使用HQL更新两个或更多实体。首先通过Hibernate会话获取Video对象并添加/删除客户

Video video = (Video)session.get(Video.class, id);
video.getCustomers().clear();//removes all the existing customers
Customer customer1 = new Customer();
customer1.getVideos().add(video)//adding video to the customer
video.getCustomers().add(customer1);
session.update(video)// this will insert row into video table, customers table and join table.