我开始在Spring Boot 1.3.1上使用Spring Data Elasticsearch,我想使用在我的数据库中使用的相同实体,它有复合键。
实体类:
@IdClass(PassengerPk.class)
@Table(name = "passenger")
@Document(indexName="passenger")
public class Passenger implements Serializable {
@Id
@ManyToOne
@JoinColumn(columnDefinition="long", name="user_id", referencedColumnName="id")
private User user;
@Id
@ManyToOne
@JoinColumn(columnDefinition="long", name="scheduler_id", referencedColumnName="id")
private Scheduler scheduler;
@Column(name = "is_active")
private Boolean isActive;
...
}
关键课程:
public class PassengerPk implements Serializable {
private Long user;
private Long scheduler;
public PassengerPk() {
}
public PassengerPk(Long user, Long scheduler) {
this.user = user;
this.scheduler = scheduler;
}
...
}
JPA Elasticsearch存储库:
public interface PassengerSearchRepository extends ElasticsearchRepository<Passenger, PassengerPk> {
}
如果我尝试编译此代码,则会收到此错误。
Caused by: java.lang.IllegalArgumentException: Unsuppored ID type class com.dualion.test.domain.PassengerPk
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getRepositoryBaseClass(ElasticsearchRepositoryFactory.java:79) ~[spring-data-elasticsearch-1.3.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryInformation(RepositoryFactorySupport.java:238) ~[spring-data-commons-1.11.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:181) ~[spring-data-commons-1.11.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) ~[spring-data-commons-1.11.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) ~[spring-data-commons-1.11.1.RELEASE.jar:na]
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean.afterPropertiesSet(ElasticsearchRepositoryFactoryBean.java:55) ~[spring-data-elasticsearch-1.3.1.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
... 71 common frames omitted
我如何修改我的代码?
由于
答案 0 :(得分:0)
我在其他地方读过相关答案并得出结论这是不可能的;然而,我的顽固性得到了我的最好,我找到了解决方案。
TLDR;强制spring使用一个新的存储库,该存储库获取复合id的hashCode并使用其String值作为其id。
步骤...
创建一个可以处理复合ID的新Repository
:
public class HashKeyedRepository<T, ID extends Serializable> extends AbstractElasticsearchRepository<T, ID> {
public HashKeyedRepository() {
super();
}
public HashKeyedRepository(ElasticsearchEntityInformation<T, ID> metadata,
ElasticsearchOperations elasticsearchOperations) {
super(metadata, elasticsearchOperations);
}
public HashKeyedRepository(ElasticsearchOperations elasticsearchOperations) {
super(elasticsearchOperations);
}
@Override
protected String stringIdRepresentation(ID id) {
return String.valueOf(id.hashCode());
}
}
请注意,这假设您已正确地在复合ID类上实现了.hashCode
。
接下来,您必须创建一个新的RepositoryFactoryBean
,它将返回此新Repository
:
public class CustomElasticsearchRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends ElasticsearchRepositoryFactoryBean<T, S, ID> {
private ElasticsearchOperations operations;
public CustomElasticsearchRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
}
public void setElasticsearchOperations(ElasticsearchOperations operations) {
super.setElasticsearchOperations(operations);
Assert.notNull(operations);
this.operations = operations;
}
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
return new ElasticsearchRepositoryFactory(operations) {
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
if (!Integer.class.isAssignableFrom(metadata.getIdType()) && !Long.class.isAssignableFrom(metadata.getIdType()) && !Double.class.isAssignableFrom(metadata.getIdType()) && metadata.getIdType() != String.class && metadata.getIdType() != UUID.class) {
return HashKeyedRepository.class;
}
return super.getRepositoryBaseClass(metadata);
}
};
}
}
最后,在启用存储库时,请指定新的RepositoryFactoryBean
类:
@EnableElasticsearchRepositories(basePackages = "xxx.xxx.repository.search", repositoryFactoryBeanClass = CustomElasticsearchRepositoryFactoryBean.class)
如果使用撰写本文时支持的任何ID(即String,UUID,Number),则此实现将回退到默认值。我不知道它是否是一个伟大的解决方案,因为有可能与.hashCode发生冲突,但它现在正在为我工作。
PS 我使用lombok
s @Data
为我自动生成.hashCode。
PPS 我见过的另一个解决方案(非java)提到了base64编码id的序列化版本(即JSON)。我认为这可以保证没有冲突,但你必须确保剥离任何多余的字符(即空格)并保证属性的顺序才能有效。