Pergunta

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?

Foi útil?

Solução

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!

Outras dicas

Currently the school might be mapped using a <many-to-one> relationship.

<many-to-one name="school" class="School">
    <column name="SCHOOL_ID" type="string" />
</many-to-one>

You can delcare one more field for SCHOOL_ID.

<property name="schoolId" column="SCHOOL_ID" type="string" insert="false" update="false"/>

Note: insert="false" update="false" is needed if a column is mapped more than once.

If you are using the (default) Jackson mapper for JSON serialization, you can annotate properties with @JsonIgnore in your entities if you don't want them to be serialized.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top