简体   繁体   中英

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

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);
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM