我正在尝试为我的mongo集合使用基类,然后让集合名称来自派生类;但是,集合名称始终只是entity
(在这种情况下,不是Derived
)。
我有以下抽象类:
public abstract class Entity {
@Id
private String id;
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
}
派生类如:
// I've also tried @TypeAlias("Derived")
@Document(collection = "Derived")
public class Derived extends Entity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
使用以下存储库:
@Component
public interface Repository<T extends Entity> extends MongoRepository<T, String> {
T findById(String id);
}
答案 0 :(得分:0)
按照这个工作示例。 让我们声明一个基础文档模型,每个文档模型必须从中继承
import java.math.BigInteger;
import org.springframework.data.annotation.Id;
public class BaseDocument {
@Id
private BigInteger id;
public BigInteger getId() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.id == null || obj == null || !(this.getClass().equals(obj.getClass()))) {
return false;
}
BaseDocument that = (BaseDocument) obj;
return this.id.equals(that.getId());
}
@Override
public int hashCode() {
return id == null ? 0 : id.hashCode();
}
}
例如,我宣布我的代码模型。 请注意,集合名称(如果与类名称不同)可以使用@Document注释设置
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "codes")
public class Code extends BaseDocument {
@Field("code")
private String code;
@Field("holiday")
private String holiday;
public Code(String code, String holiday) {
super();
this.code = code;
this.holiday = holiday;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getHoliday() {
return holiday;
}
public void setHoliday(String holiday) {
this.holiday = holiday;
}
@Override
public String toString() {
return "Code [code=" + code + ", holiday=" + holiday + "]";
}
}
此时,您可以使用MongoOperations和其他类轻松地使用这些类,以便对数据库和集合进行操作。 MongoOperations具有操作集合所需的所有方法,例如:findOne,findAll等。 为了清楚起见,您可以通过以下方式使用MongoOperations:
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient(), "yourDBname"));
我强烈建议实现一个对集合进行操作的服务类,就像这个一样,而不是直接在它上面运行:
import java.util.List;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
public class CodesService extends BaseService implements BaseOperations{
@Override
public BaseDocument findOne(Long id) {
Query query = new Query(Criteria.where("code").is("8"));
return this.mongoOps.findOne(query, Code.class);
}
@Override
public List<? extends BaseDocument> findAll() {
List<Code> subs = this.mongoOps.findAll(Code.class);
List<? extends BaseDocument> codes = subs;
return codes;
}
}
该类实现以下接口:
import java.util.List;
public interface BaseOperations {
public BaseDocument findOne(Long id);
public List<? extends BaseDocument> findAll();
}
希望这可能会有所帮助。
答案 1 :(得分:0)
尝试实现如下所示的Repository<T extends Entity>
接口
public interface SomeInterfaceName extends Repository<Derived>{
}
现在尝试@Autowire
的{{1}}回购
它将为您提供所需的输出