弹簧数据休息与复合主键

时间:2017-05-03 14:42:33

标签: java spring spring-data-rest composite-primary-key

我使用spring data rest for crud。但是当实体具有复合主键时,我不知道如何通过提供主键来获取实体。

河流类:

@Entity
public class River {
    private RiverPK id;
    private Double length;
    private Timestamp date;
    private String comment;


    @Basic
    @Column(name = "length")
    public Double getLength() {
        return length;
    }

    public void setLength(Double length) {
        this.length = length;
    }

    @Basic
    @Column(name = "date")
    public Timestamp getDate() {
        return date;
    }

    public void setDate(Timestamp date) {
        this.date = date;
    }

    @Basic
    @Column(name = "comment")
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Id
    public RiverPK getId() {
        return id;
    }

    public void setId(RiverPK id) {
        this.id = id;
    }
}

RiverPK课程:

@Embeddable
public class RiverPK implements Serializable {
    private String name;
    private int upcode;
    private int downcode;

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "upcode")
    public int getUpcode() {
        return upcode;
    }

    public void setUpcode(int upcode) {
        this.upcode = upcode;
    }

    @Column(name = "downcode")
    public int getDowncode() {
        return downcode;
    }

    public void setDowncode(int downcode) {
        this.downcode = downcode;
    } 

}

RiverDAO课程:

@RepositoryRestResource(path = "river")
public interface RiverDAO extends JpaRepository<River, RiverPK> {
}

然后我可以通过调用get http://localhost:8080/river/来获取河流数据,并通过呼叫帖http://localhost:8080/river/ {river json}

创建新的实体到db 河流json是:

id": {

    "name": "1",
    "upcode": 2,
    "downcode": 3

},
"length": 4.4,
"date": 1493740800000,
"comment": "6"
}

在spring data rest doc中,它应该能够调用get localhost:8080 / river / 1(主键)来获取主键为1的实体。当实体只有一个主键时,这可以工作。但我的实体河有复合主键作为RiverPK。如果我调用get localhost:8080 / river / {name =&#39; 1&#39;,upcode = 2,downcode = 3},则返回错误&#34;没有找到能够从类型[java。 lang.String]输入[com.example.db.entity.RiverPK]&#34;,我的意思是使用{name =&#39; 1&#39;,upcode = 2,downcode = 3}作为字符串,但不是RiverPK类型。

问题是如何使用复合主键调用get \ put \ delete作为其他正常实体?

2 个答案:

答案 0 :(得分:3)

你可以看一下jira问题: https://jira.spring.io/browse/DATAREST-598

此评论对您来说可能特别有趣

https://jira.spring.io/browse/DATAREST-598?focusedCommentId=117740&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-117740

在那里,您还可以找到示例项目的github link。它使用BackendIdConverter将复合键转换为字符串并返回。因此,技巧是将您的复合ID转换为可用作路径段的字符串。

这个答案对你来说也很有意思https://stackoverflow.com/a/31830586/5371736

答案 1 :(得分:-3)

Customizing HATEOAS link generation for entities with composite ids学习后,我找到了一个更通用的解决方案。

首先,创建一个SpringUtil来从spring获取bean。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

然后,实现BackendIdConverter。

import com.alibaba.fastjson.JSON;
import com.example.SpringUtil;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.spi.BackendIdConverter;
import org.springframework.stereotype.Component;

import javax.persistence.EmbeddedId;
import javax.persistence.Id;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.net.URLEncoder;

@Component
public class CustomBackendIdConverter implements BackendIdConverter {

    @Override
    public boolean supports(Class<?> delimiter) {
        return true;
    }

    @Override
    public Serializable fromRequestId(String id, Class<?> entityType) {
        if (id == null) {
            return null;
        }

        //first decode url string
        if (!id.contains(" ") && id.toUpperCase().contains("%7B")) {
            try {
                id = URLDecoder.decode(id, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        //deserialize json string to ID object
        Object idObject = null;
        for (Method method : entityType.getDeclaredMethods()) {
            if (method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class)) {
                idObject = JSON.parseObject(id, method.getGenericReturnType());
                break;
            }
        }

        //get dao class from spring
        Object daoClass = null;
        try {
            daoClass = SpringUtil.getBean(Class.forName("com.example.db.dao." + entityType.getSimpleName() + "DAO"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //get the entity with given primary key
        JpaRepository simpleJpaRepository = (JpaRepository) daoClass;
        Object entity = simpleJpaRepository.findOne((Serializable) idObject);
        return (Serializable) entity;

    }

    @Override
    public String toRequestId(Serializable id, Class<?> entityType) {
        if (id == null) {
            return null;
        }

        String jsonString = JSON.toJSONString(id);

        String encodedString = "";
        try {
            encodedString = URLEncoder.encode(jsonString, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodedString;
    }
}

之后。你可以做你想做的事。

下面有一个示例。

  • 如果实体具有单一属性pk,则可以使用 localhost:8080 / demo / 1正常。根据我的代码,假设pk 有注释&#34; @ Id&#34;。
  • 如果实体已经组成pk,假设pk是demoId类型,并且具有 注释&#34; @ EmbeddedId&#34;,您可以使用localhost:8080 / demo / {demoId 获取/放置/删除json}。你的自我链接也一样。