简体   繁体   English

多对多的 JPQL 查询

[英]JPQL query for many to many

I want to write JPQL query for fetching rows that have id values of a Many to Many table.我想编写 JPQL 查询来获取具有多对多表的 id 值的行。 I have two classes:我有两个班级:

@Entity
@Table(name = "farm")
@Getter
public class Farm {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "name")
    private String name;

    @ManyToOne()
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "customer_id", referencedColumnName = "id")
    private Customer customer;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "user_farm", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "farm_id"))
    private List<User> users = new ArrayList<>();
}

and

@Entity
@Table(name = "user")
@Getter
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;

    @ManyToMany(mappedBy = "users")
    private List<Farm> farms = new ArrayList<>();

    @Column(name = "is_admin")
    private Boolean isAdmin;
}

and a repository:和一个存储库:

public interface FarmRepository extends JpaRepository<Farm, Long> {

    @Query("select f from Farm f join f.users u where u.id=:userId")
    List<Farm> findByUserId(@Param("userId") Long userId);
}

But, I get this hibernate error:但是,我收到此休眠错误:

ERROR: column user2_.id does not exist

How do I properly write the jpql query so it works?如何正确编写 jpql 查询以使其正常工作?

The error hints to the fact that query got executed, but your database table does not contain the id column.该错误提示查询已执行,但您的数据库表不包含 id 列。

  1. Go to your application.properties file and add this to see SQL in your console:转到您的 application.properties 文件并添加以下内容以在您的控制台中查看 SQL:

    spring.jpa.show-sql=true spring.jpa.show-sql=true

    spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true

  2. Execute your test and see if the correct SQL gets generated.执行您的测试并查看是否生成了正确的 SQL。

  3. Think about who creates your database?想想谁创建了您的数据库? Automatic hbm2ddl schema creation?自动创建 hbm2ddl 模式? Liquibase?液碱? Flyway?飞道? schema.sql?模式.sql? Fix your database tables.修复您的数据库表。

I think I might have a similar problem too.我想我也可能有类似的问题。 The table name "user" very often is reserved, you can't use it.表名“user”经常被保留,你不能使用它。 Try to rename it to "person" or something else.尝试将其重命名为“person”或其他名称。 Probably it'll help.可能会有所帮助。

https://stackoverflow.com/a/30157994/9890613 https://stackoverflow.com/a/30157994/9890613

As far as I know, @ManyToMany needs bi-directional relation, but at your example that it's single.据我所知, @ManyToMany需要双向关系,但在你的例子中它是单一的。 Lets see at the structure:让我们看看结构:

User:
Long id;
Farm:
Long id;
User_farm:
Long r_user_id;
Long r_farm_id;

So then the @JoinTable must be:那么@JoinTable必须是:

  @ManyToMany(cascade = CascadeType.ALL)
  @JoinTable(
      name = "USER_FARM",
      joinColumns = @JoinColumn(name = "r_user_id", referencedColumnName = "id"),
      inverseJoinColumns = @JoinColumn(name = "r_farm_id", referencedColumnName = "id"))
  private List<User> users;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM