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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:27:43+00:00 2026-06-03T15:27:43+00:00

I’m working on a REST application with Spring-data Hibernate etc.. I’ve setup all my

  • 0

I’m working on a REST application with Spring-data Hibernate etc.. I’ve setup all my basic actions but now I want to do something specific.
When i retrieve a list of items which have a foreign key to another table in my database, hibernate will return the entire row of that foreign key. For example:

[ {
  "id":1, 
  "school": {"id":1, "name":"De regenboog", password":"800e30e9bb3b82ce955a127650d835d0", "street":"Plantaanstraat", "number":"2", "zipCode":"1234AS", "city":"Amsterdam", "contactName":"Arends", "contactPhoneNumber":"06-45648466", "contactEmail":"arends@regenboog.nl"}, 
  "name":"Groep", 
  "dateCreated":"2012-04-25"
  }
]

(These are all fictional data by the way)

Now the thing is is don’t want the school to be returned in its entirety. I just want to show the school_id.
I’ve searched around the web and read some things about “service level mapping” but I was unable to find any examples. I’ve built my application with a

controller -> service -> dao -> repository

setup.

I hope you guys can help me out! (let me know if you need more source code as well).

Thanks alot

EDIT

The thing I want to add is that my MySql table looks like this:

ID | SCHOOL_ID | NAME | DATE_CREATED

So what i’d like to have returned is just the plain school_id instead of the object school (in this situation)

EDIT2
I’m working on @Dandy answer and I want to show the code I have now:

@ManyToOne
@JoinColumn(name = "SCHOOL_ID")
private School school;

@Column(name = "SCHOOL_ID", insertable = false, updatable = false)
private long schoolId;

public long getSchoolId() {
    return schoolId;
}

public void setSchoolId(long schoolId) {
    this.schoolId = schoolId;
}

public School getSchool() {
    return school;
}

public void setSchool(School school) {
    this.school = school;
}

When i change the code like Danny suggested, I get the result that I want.. almost.
This is what I get if I run the query now:

[ {
  "id":1, 
  "school": {"id":1, "name":"De regenboog", password":"800e30e9bb3b82ce955a127650d835d0", "street":"Plantaanstraat",  "number":"2", "zipCode":"1234AS", "city":"Amsterdam", "contactName":"Arends", "contactPhoneNumber":"06-45648466", "contactEmail":"arends@regenboog.nl"},    
  "schoolId": 1  
  "name":"Groep", 
  "dateCreated":"2012-04-25"
  }
]

The thing is that I want to disable the school for this particular query. Is that possible?

  • 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-03T15:27:44+00:00Added an answer on June 3, 2026 at 3:27 pm

    A few days have passed and I found a solution that worked for me. I made some new classes IE SchoolDTO (DTO stands for Data Transfer Object). Inside these classes I only added the values I wanted to show to the front end. ie:

    public class ClassesDTO {
    
        private long id;
        private long schoolId;
        private String schoolName;
        private String name;
        private Date dateCreated;
    
        //Getters and setters...
    }
    

    Instead of showing the entire School object I only included it’s ID and NAME. Inside this class there are only getters and setters. Then I make a mapper which will map the original Classes to the new DTO Class. ie.

    public ClassesDTO mapToDTO(Classes classes) {
        ClassesDTO classesDTO = new ClassesDTO();
        classesDTO.setId(classes.getId());
        classesDTO.setSchoolId(classes.getSchool().getId());
        classesDTO.setSchoolName(classes.getSchool().getName());
        // etc...
    

    Then in my service layer I return the new mapped DTO object to my controller instead of the normal Classes object.

    And that’s all actually. I also have a mapping if I need to return a java.util.List and the other way around if I want to insert data into my database without having to declare the entire school. I’ll show a quick example of this just someone is wondering how I did this:

    public Classes mapFromDTOCreate(ClassesCreateDTO classesCreateDTO){
        Classes classes = new Classes();
        classes.setSchool(schoolDAO.findSchoolById(classesCreateDTO.getSchoolId()));
        classes.setName(classesCreateDTO.getName());
        classes.setDateCreated(classesCreateDTO.getDateCreated());
        return classes;
    }
    

    This new classes object I can safely return to my DAO which hibernate can save to my database cause of the ORM (Object relational mapping).

    If anyone has any questions please let me know!

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.