Ursus Schneider
Ursus Schneider

Reputation: 467

Return @id fields from Spring Data JpaRepository

I have inherited a MASSIVE Java application and am having quite a few problems trying to find my way around it. I have a specific problem regarding Spring JpaRepository. Please note that I have just started in Spring and am not that sure footed yet.

I have a repository with the @RepositoryRestResource annotations.

@RepositoryRestResource
public interface GoodsReceiptRepository extends JpaRepository<GoodsReceipt, GoodsReceiptId> {

I have the @Entity as well:

@Entity
@Table(name = AvisoPosition.TABLE)
@IdClass(AvisoPositionId.class)
public class AvisoPosition implements Serializable {

and

@Entity
@Table(name = GoodsReceipt.TABLE)
@IdClass(GoodsReceiptId.class)
public class GoodsReceipt implements Serializable { 

any fields I have with the @Id annotation are not returned in the JSON response:

@Id @Column(name = "LGNTCC")
private String accountingArea;

How do I get these ID fields?

If I remove the @Id I get what I want but I do not dare do that as I cannot judge what effect that will have on the application.

Cheers

Upvotes: 0

Views: 1811

Answers (2)

Sunshine
Sunshine

Reputation: 68

Try to create a DTO for each entity, then avisoPostionDTO.setId(avisoPosition.getId()); and you just return the DTO of each entity.

Upvotes: 0

Marcos Barbero
Marcos Barbero

Reputation: 1119

It seems that you're using Spring Data Rest what you're seeing is the default behavior for it.

You can customize this behavior by doing the following:

import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(YourClassNameGoesHere.class);
    }
}

Upvotes: 2

Related Questions