gpt4 book ai didi

java - org.hibernate.hql.internal.ast.ErrorCounter - 第 1 行 :199: unexpected token: JOIN

转载 作者:行者123 更新时间:2023-11-29 11:56:33 27 4
gpt4 key购买 nike

我有两个实体

@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
}

我创建了一条记录

Video title: Stand Up for the the Champs Customers 1-John, 2-James

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

Video title: Stand Up for the ... Customers 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)

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

最佳答案

您无法使用 HQL 更新两个或多个实体。首先通过Hibernate session 获取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.

关于java - org.hibernate.hql.internal.ast.ErrorCounter - 第 1 行 :199: unexpected token: JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33112281/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com