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?
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:
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.
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.Listand 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: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!