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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:06:25+00:00 2026-06-07T14:06:25+00:00

I’ve recently started using Hibernate and am trying to get my head around all

  • 0

I’ve recently started using Hibernate and am trying to get my head around all the annotations and ensure I do things properly. I have two tables ‘user’ and ‘user_friends’ that are similar to the below

+------+------+-------+------+
| id   | name | email | etc. |
+------+------+-------+------+

and the user friends table

+--------+---------+----------+
| userid | buddyid | accepted |
+--------+---------+----------+

Now in SQL I ran a query that looked similar to

SELECT u.id AS id, u.name AS username, u.email AS email FROM user_friends 
INNER JOIN users AS u ON u.id = '1' WHERE buddyid = '2' AND ACCEPTED = 1 UNION ALL 
SELECT u.id as id, u.name AS username, u.email AS email FROM user_friends
INNER JOIN users AS u ON u.id = '2' WHERE buddyid = '1' AND ACCEPTED = 1;

I’ve got two classes in Java set-up in a fashion similar to this

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

  @Id
  @GeneratedValue
  private int id;

  private int name;
  private int email;

  private DateTime registerDate;
  private DateTime lastActivity;

  private int currency;
  private int seasonCurrency;

  @OneToMany(fetch=UserBuddy.class, mappedBy="user", fetch=FetchType.LAZY)
  @JoinColumn(name="userid")
  @Filter(name="messengerBuddyFilter", condition="accepted=1")
  private Set<UserBuddy> _buddies;
}

@Entity
@Table(name="user_friends")
public class UserBuddy {

   private int id;
   private int name;
   private int email;
}

I asked this question before but still haven’t been able to get this working how I would like it. I need to be able to return a set of UserBuddy.class that contains the name and email of that Buddy and nothing else, not their register time etc (by using a User class to map). I’m also having difficulty ensuring it only returns buddies that have accepted the request (ACCEPTED=1)

Can anyone offer any suggestions?

  • 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-07T14:06:28+00:00Added an answer on June 7, 2026 at 2:06 pm

    You need to use native queries if you are using JPA (and named native queries with hibernate and JPA are problematic) or IIRC sql queries if just using pure hibernate.

    This might work… just doing this quickly, use it as a place to start. Say you have an entity manager in JPA named em defined as a class member variable

    @PersistenceContext(unitName = "MyPersistenceUnit_PU")
    private EntityManager em;
    

    …

    some method…

    List<UserBudy> buddyList = new <>ArrayList();
    Query q = createNativeQuery("SELECT u.id AS id, u.name AS username, u.email AS email "
                              + "FROM user_friends "
                              + "INNER JOIN users AS u "
                              + "ON u.id = '1' "
                              + "WHERE buddyid = '2' "
                              + "AND ACCEPTED = 1 "
                              + "UNION ALL "
                              + "SELECT u.id as id, u.name AS username, u.email AS email "
                              + "FROM user_friends "
                              + "INNER JOIN users AS u "
                              + "ON u.id = '2' "
                              + "WHERE buddyid = '1' "
                              + "AND ACCEPTED = 1");
    List<UserBudy> buddyList = (List<UserBudy>)em.getResultList);
    

    This example is not a “named” native query. This is done inline similar to POJDBC (plain old jdbc). Like I said, if you were going to try to create a NamedNativeQuery using JPA and Hibernate you would get a “something or other not implemented yet exception”. Hibernate hasn’t supported named native queries with JPA forever. It is still in their bug tracker somewhere (for 5 or 6 years already) and it looks like they don’t give a rats ass about fixing it. Overall though, I’ve had less issues with Hibernate when working with PostgreSQL than with Eclipselink, so I can put up with it for now. 🙂

    IIRC if you were to do this with pure hibernate you would create a Hibernate session object instead of an JPA EM object and use createSQLQuery instead. Other than that would be nearly the same.

    i.e.

    session.createSQLQuery("...");
    

    If this didn’t help enough, you should now at least have enough info to figure it out.

    And here is a link if you want to try what you are after using pure JPA. Look for the “line item summary” example about half way down this article.

    http://www.oracle.com/technetwork/articles/vasiliev-jpql-087123.html

    Remember when you are dealing with JPA you are joining the entities, not the tables. Anyway, that article is one of the clearest I’ve ever found showing how a lot of it works. Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.