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

  • SEARCH
  • Home
  • 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 1076041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:21:21+00:00 2026-05-16T21:21:21+00:00

I want to create Many-One Mapping between two tabels, Expense(ID, NAME, CATEGORY) and Category(ID,

  • 0

I want to create Many-One Mapping between two tabels, Expense(ID, NAME, CATEGORY) and
Category(ID, NAME).
In my class i have created a field ‘Category category’ and its setters and getters.
I did them after seeing some stuff from internet. What are all the changes i have to do in my Category.java class. For now, its looks like,

public class Category{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;

public Category() {
}

public int getCatId() {
    return this.catId;
}

public void setCatId(int catId) {
    this.catId = catId;
}

public String getCatName() {
    return this.catName;
}

public void setCatName(String catName) {
    this.catName = catName;
}

}

I dont want to do mappings with xml config. I think, annotations is good for a beginner like me.

And my Old! SQL query looks like,

SELECT EXPENSES.EXPNS_ID, EXPENSES.CAT_ID, EXPENSES.NAME, CATEGORY.CAT_NAME FROM EXPENSES INNER JOIN CATEGORY ON EXPENSES.CAT_ID = CATEGORY.CAT_ID WHERE USER_NAME="+currentUserName

How to use inner join in Hibernate?

Any Suggestions!!

Thanks!


Update

Thanks for all answerers,
I tried what you told and it returns a empty list.

To, test i set the ‘userName=Tamil’ which is in the table.
The query generated by Hibernate is looks like below,

select expens0_.expnsId as expnsId1_, expens0_.catId as catId1_, expens0_.category_catId as category7_1_, expens0_.userName as userName1_ from Expens expens0_ inner join Category category1_ on expens0_.category_catId=category1_.catId where expens0_.userName=?

As a beginner, i have some doubts in JPQL, I want catName from Category[catId, catName] table. And the catId is also available in Expens[expnsId, catId, userName].

By adding the below lines in Expens.java class, how it will give me catName along with the other variables in the Expens table.

@ManyToOne
private Category category

// getters, setters

I cant able to understand it. Without understanding this i cant move further, i have to give more mappings in my project. If clear with this mapping, i can move to the rest with confidence.

The query i used is pascal’s version: Query query = hSession.createQuery("SELECT e FROM Expens e JOIN e.category c WHERE e.userName = :userName").setParameter("userName", userName);

For me, the query generated by hibernate is looks like same as my Old SQl query. I cant able to find problem here.

  • 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-05-16T21:21:22+00:00Added an answer on May 16, 2026 at 9:21 pm

    Actually, a big part of the documentation that would be useful in your case is located in the Hibernate Annotations Reference Guides (links provided below). Reading it would be very worth it.

    That being said, regarding your specific question, the simplest possible mapping would be:

    @Entity
    public class Expense {
        @Id @GeneratedValue
        private Long;
    
        @ManyToOne
        private Category category
    
        // getters, setters
        ...
    }
    

    That’s all.

    If you want to make it bi-directional, you’ll have to add a OneToMany on the other side (and don’t forget the mappedBy element since the association is bidirectional):

    @Entity
    public class Category {
        @Id @GeneratedValue
        private Long id;
    
        @OneToMany(mappedBy="category")
        private Set<Expense> expenses = new HashSet<Expense>();
        ....
    }
    

    And a possible JPQL query would be:

    SELECT e FROM Expense e JOIN e.category c WHERE e.username = :username  
    

    Update: Hibernate and JDBC are different. With Hibernate, you need to think objects and the above HQL query (which was more an example) will actually return a List<Expense>. To get a category name, iterate over the results and navigate through the association. For example:

    List<Expense> expenses = ... // some code to retrieve a list by username
    
    for (Expense expense : expenses) {
        System.out.println(expense.getCategory().getName());
    }
    

    References

    • 2.2. Mapping with JPA (Java Persistence Annotations)
    • 2.2.5.2. Many-to-one
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with one-to-many mapping. Two tables questions(PK(id), text) and answers(PK(id, questionId), text)
I want to create a view in a one-to-many relation. Here are my relations:
Basically I want to create one large object of many object in JavaScript. Something
I have created Twitter bots for many geographic locations. I want to allow users
I have two related tables, MANY and ONE. An integer ONE_ID column on the
I want to create one to many relation using Google DataStore entities Something like:
I want to create a one to many relationship. My setup is something like:
i want create image animation , i have 50 images with png format now
I've the AnagraficaBase hbm file: <?xml version=1.0 encoding=utf-8?> <hibernate-mapping xmlns=urn:nhibernate-mapping-2.2> <class entity-name=AnagraficaBase table=anagrafica> <id
I have an edit object view that contains a formset(one or many if this

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.