MetamodelImpl metaModel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel();
EmbeddableType keyObj = null;
Object key = null;
id = java.net.URLDecoder.decode(id, "UTF-8");
if (metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) {
keyObj = metaModel.embeddable(entityMetadata.getIdAttribute().getBindableJavaType());
key = JAXBUtils.toObject(id, keyObj.getJavaType(), mediaType);
} else {
ObjectMapper mapper = new ObjectMapper();
key = mapper.convertValue(id, entityMetadata.getIdAttribute().getBindableJavaType());
}