Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:50:07+00:00 2026-06-13T13:50:07+00:00

I would like to make a Join query using Jpa repository with annotation @Query.

  • 0

I would like to make a Join query using Jpa repository with annotation @Query.

I have two tables:

 table user 
 with iduser,user_name 

and:

 table area 
 with idarea, area_name and iduser

The native query is:

 SELECT
 u.user_name 
 FROM
  user as u 
  INNER JOIN area as a ON a.iduser = u.iduser
  WHERE
  a.idarea = 4

Now I have a Table Hibernate entity
User and Area

So I tried with UserRespository

@Query(SELECT  u.userName FROM  User u 
  INNER JOIN Area a ON a.idUser = u.idUser
  WHERE
  a.idArea = :idArea)
List<User> findByIdarea(@Param("idArea") Long idArea);

The Log says:

unexpected token:

Any Idea, please?

My table Entity

#User Table
@Entity
@Table(name="user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long idUser;
    private String userName;

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="iduser")
    public Long getIdUser() {
        return idUser;
    }

    public void setIdUser(Long idUser) {
        this.idUser = idUser;
    }

    @Column(name="user_name")
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

#AREA table

@Entity
@Table(name="area")
public class Area  implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long idArea;
    private String areaName;
    private Long idUser;

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="idarea")
    public Long getIdArea() {
        return idArea;
    }

    public void setIdArea(Long idArea) {
        this.idArea = idArea;
    }

    @Column(name="area_name")
    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    @Column(name="iduser")
    public Long getIdUser() {
        return idUser;
    }

    public void setIdUser(Long idUser) {
        this.idUser = idUser;
    }       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:50:08+00:00Added an answer on June 13, 2026 at 1:50 pm

    You are experiencing this issue for two reasons.

    • The JPQL Query is not valid.
    • You have not created an association between your entities that the underlying JPQL query can utilize.

    When performing a join in JPQL you must ensure that an underlying association between the entities attempting to be joined exists. In your example, you are missing an association between the User and Area entities. In order to create this association we must add an Area field within the User class and establish the appropriate JPA Mapping. I have attached the source for User below. (Please note I moved the mappings to the fields)

    User.java

    @Entity
    @Table(name="user")
    public class User {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="iduser")
        private Long idUser;
    
        @Column(name="user_name")
        private String userName;
    
        @OneToOne()
        @JoinColumn(name="idarea")
        private Area area;
    
        public Long getIdUser() {
            return idUser;
        }
    
        public void setIdUser(Long idUser) {
            this.idUser = idUser;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public Area getArea() {
            return area;
        }
    
        public void setArea(Area area) {
            this.area = area;
        }
    }
    

    Once this relationship is established you can reference the area object in your @Query declaration. The query specified in your @Query annotation must follow proper syntax, which means you should omit the on clause. See the following:

    @Query("select u.userName from User u inner join u.area ar where ar.idArea = :idArea")
    

    While looking over your question I also made the relationship between the User and Area entities bidirectional. Here is the source for the Area entity to establish the bidirectional relationship.

    Area.java

    @Entity
    @Table(name = "area")
    public class Area {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="idarea")
        private Long idArea;
    
        @Column(name="area_name")
        private String areaName;
    
        @OneToOne(fetch=FetchType.LAZY, mappedBy="area")
        private User user;
    
        public Long getIdArea() {
            return idArea;
        }
    
        public void setIdArea(Long idArea) {
            this.idArea = idArea;
        }
    
        public String getAreaName() {
            return areaName;
        }
    
        public void setAreaName(String areaName) {
            this.areaName = areaName;
        }
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have db in MySQL, with two tables and I need to make query
I would like make a script using PHP (probably need JS) to send POST
I would like to make my app freeware, but if a user is willing
I have two tables: Table Users UserId Name 1 John 2 Alice 3 Tom
I have a troublesome SQL query that I'm trying to make using Entrinsik's Informer.
Anyone knows a good way to make UNION query in CakePHP? I would like
I have this table below How do I make the output like this I
I would like make an extension method for the generic class A which takes
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make sure my classes are robust - even if they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.