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 8191253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:52:40+00:00 2026-06-07T03:52:40+00:00

I am implementing a RESTful Web Service using Jersey. I use hibernate to communicate

  • 0

I am implementing a RESTful Web Service using Jersey. I use hibernate to communicate with the database (mySQL). My hibernate resource classes includes:

@Entity
public class Activity {

    @Id
    @GeneratedValue
    private long id;

@ManyToOne
    @JoinTable(name="category_activity",
    joinColumns={@JoinColumn(name="activities_id")},
    inverseJoinColumns={@JoinColumn(name="Category_id")})
    private Category category;
}

and the Category class:

@Entity
public class Category {

    @Id
    @GeneratedValue
    private long id;

    @OneToMany
    @Fetch(FetchMode.JOIN)
    @JoinTable(name = "category_activity",
    joinColumns = { @JoinColumn(name = "Category_id") }, 
    inverseJoinColumns = { @JoinColumn(name = "activities_id") })
    @JsonIgnore
    private Collection<Activity> activities;
}

I used this query to fetch the ativities:

session.createQuery("from Activity a join a.category cs where cs.id= :categoryId order by a.key").setLong("categoryId", categoryId).list();

The result in JSON format is not right like:

[[{"id":26,"key":"other","name":"Other","cost":100.0,"category":{"id":10,"name":"General","description":""}},{"id":10,"name":"General","description":""}]]

As you see category is printed 2 times and we have a extra [] around it.
When I use another mechanism of One-To-Many relation in Category class like:

@OneToMany(targetEntity = Activity.class, mappedBy = "category", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Collection<Project> activities;

And in Activity class:

@ManyToOne(optional = false)
    private Category category;

And this query:

session.createQuery("from Activity as a where a.category.id= :categoryId order by a.key").setLong("categoryId", categoryId).list();

Everything works fine. But I have to use join table because I do not suppose to change the database.

The proper result should look like:

[{"id":26,"key":"other","name":"Other","cost":100.0,"category":{"id":10,"name":"General","description":""}}]

I appreciate for any help.

  • 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-07T03:52:41+00:00Added an answer on June 7, 2026 at 3:52 am

    Define the join table on the many side, but don’t define it once again on the one side. This creates two unidirectional associations mapped with the same table instead of one bidirectional association.

    A bidirectional association always has an owner side (where you specify the join column or join table to use, and an inverse side which says hat it’s the inverse of the other side by using the mappedBy attribute:

    public class Activity {
    
        @ManyToOne // owner side: it doesn't have mappedBy, and can decide how the association is mapped: with a join table
        @JoinTable(name="category_activity",
                   joinColumns={@JoinColumn(name="activities_id")},
                   inverseJoinColumns={@JoinColumn(name="Category_id")})
        private Category category;
    }
    
    public class Category {
        @OneToMany(mappedBy = "category") // inverse side: it has a mappedBy attribute, and can't decide how the association is mapped, since the other side already decided it.
        @Fetch(FetchMode.JOIN)
        @JsonIgnore
        private Collection<Activity> activities;
    }
    

    EDIT:

    Also, your query should only select the activity, and not all the entities joined by the query, by adding a select clause:

    select a from Activity as a where a.category.id= :categoryId order by a.key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a Restful Web Service using Jersey. I have a package using
I am implementing a Restful Web Service using Jersey. I want to show index.jsp
I am implementing a Restful Web Service in java (JAX-RS) using Jesey. I run
I am implementing a RESTful web service that accesses a database. Entities in the
Before implementing j_security_check using MySQL realm authentication in my web app. I had the
I'm implementing a RESTful web service in python and would like to add some
I have succesfully implemented a RESTful Web Service using the .NET 4.0 framework with
I want to implement spring security on a jersey Restful web service, both on
The scenario is: I'm implementing a RESTful web-service that will act as a cache
I am exploring the idea of implementing a web service api using WCF Data

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.