简体   繁体   中英

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. 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?

The error hints to the fact that query got executed, but your database table does not contain the id column.

  1. Go to your application.properties file and add this to see SQL in your console:

    spring.jpa.show-sql=true

    spring.jpa.properties.hibernate.format_sql=true

  2. Execute your test and see if the correct SQL gets generated.

  3. Think about who creates your database? Automatic hbm2ddl schema creation? Liquibase? Flyway? schema.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. Try to rename it to "person" or something else. Probably it'll help.

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

As far as I know, @ManyToMany needs bi-directional relation, but at your example that it's single. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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