我的要求是:我必须创建一个AccountRepository接口,我必须在我的AccountRepositoryImpl本身实现所有方法,所以我该怎么做呢?
例:
1)界面
/* this is the interface */
public interface AccountRepository extends JpaRepository
{
List<Account> getAllAccounts();
}
Run Code Online (Sandbox Code Playgroud)
2)实施?
public class AccountRepositoryImpl implements AccountRepository
{
public List<Account> getAllAccounts() {
// now what?
}
}
Run Code Online (Sandbox Code Playgroud) 我是Solr的新手.我正在为我的应用程序实现自动完成功能.我在solr中配置了必需的字段,并创建了一个自定义请求处理程序/建议.我发现通过solr java client solrj访问它很棘手.我对solr的弹簧数据甚至没问题.请有人帮助从solr java客户端访问我的自定义请求处理程序.
使用mongodb shell,我能够执行一个检索整个文档的聚合查询.为了做到这一点,我使用$$ ROOT变量.
db.reservations.aggregate([
{ $match : { hotelCode : "0360" } },
{ $sort : { confirmationNumber : -1 , timestamp: -1 } },
{ $group : {
_id : "$confirmationNumber",
timestamp :{$first : "$timestamp"},
fullDocument :{$first : "$$ROOT"}
}}
])
Run Code Online (Sandbox Code Playgroud)
它检索内容为confirmationNumber,timestamp,fullDocument的对象.fullDocument是整个文档.
我想知道是否可以对Spring-Data和聚合框架做同样的事情.
我的java代码是:
TypedAggregation<ReservationImage> aggregation = newAggregation(
ReservationImage.class,
match(where("hotelCode").is(hotelCode)),
sort(Direction.DESC,"confirmationNumber","timestamp"),
group("confirmationNumber").
first("timestamp").as("timestamp").
first("$$ROOT").as("reservationImage"));
List<myClass> items = mongoTemplate.aggregate(
aggregation,
myClass.class).getMappedResults();
Run Code Online (Sandbox Code Playgroud)
错误是:org.springframework.data.mapping.PropertyReferenceException:找不到类型myClass的属性$$
你有什么想法?
谢谢.
我有一个实体A(ManyToMany,direct,lazy),其中包含对实体B集合的引用。
我需要查询数据库,根据它引用的类B的元素搜索实体A。如何用Spring规范来做到这一点?
我正在使用spring boot 1.1.5构建一个MongoDb rest应用程序.我创建了一个存储库:
@RepositoryRestResource(collectionResourceRel = "SourceProductV1Repository", path = "SourceProductV1Repository")
public interface SourceProductV1Repository extends MongoRepository<SourceProductV1Model, String> {
public List<SourceProductV1Model> findByUser (@Param("user") MongoUser user);
}
Run Code Online (Sandbox Code Playgroud)
我定义了一个转换器将String转换为MongoUser(转换器在spring上下文中正确注册):
@Component
public class String2MongoUserConverter implements Converter<String, MongoUser>{
private static final Log LOGGER = LogFactory.getLog(String2MongoUserConverter.class.getName());
@Override
public MongoUser convert(String s) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我尝试访问时 http://localhost:8080/SourceProductV1Repository/search/findByUser?user=test
我收到以下错误:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type @org.springframework.data.repository.query.Param com.ddelizia.model.MongoUser
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:291)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:177)
at org.springframework.data.rest.core.invoke.ReflectionRepositoryInvoker.prepareParameters(ReflectionRepositoryInvoker.java:265)
at org.springframework.data.rest.core.invoke.ReflectionRepositoryInvoker.invokeQueryMethod(ReflectionRepositoryInvoker.java:230)
at org.springframework.data.rest.webmvc.RepositorySearchController.executeQueryMethod(RepositorySearchController.java:236)
at org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(RepositorySearchController.java:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native …Run Code Online (Sandbox Code Playgroud) 我是基于Spring的项目的新手,我需要在OneToMany中创建订单和发票之间的实体关系映射,并尝试下面的映射,但最终会出现映射错误,请指点我解决这个问题.
@Entity
@Table(name="Customers")
public class Customers implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "CustomerId", nullable = false)
private Long CustomerId;
@OneToMany(cascade=CascadeType.ALL, mappedBy="Customers")
private Set<Orders> Orders = new HashSet<Orders>();
}
@Entity
@Table(name="Orders")
public class Orders implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "orderId", nullable = false)
private Long orderId;
@JoinColumn(name="CustomerId")
@ManyToOne …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Data JPA(内部的Hibernate).我有两个实体测验和问题.OneToMany他们之间的关系.测验有问题清单.如何获取有关测验和问题列表大小的信息?如果我这样写:
Quiz quiz = quizRepository.findOne(1);
int questionCount = quiz.getQuestions().size();
Run Code Online (Sandbox Code Playgroud)
Hibernate生成两个查询:
但我只需要测验信息和问题的大小.
如果没有第二次选择,我怎么能这样做?
我正在为扫描mongo存储库指定单个基础包
<mongo:repositories base-package="com.example.conditions.dao"/>
Run Code Online (Sandbox Code Playgroud)
现在我的项目中包含了另一个模块,我希望能够指定该软件包也可以通过spring扫描存储库.
<mongo:repositories base-package="com.example.cf.common.dao"/>
Run Code Online (Sandbox Code Playgroud)
如何指定弹簧扫描的两个包?
我正在使用Spring Framework JDBC来处理PostgreSQL上的所有数据库作业.现在我想将我的读写分为主服务器和从服务器.我可以在不涉及Hibernate等其他框架的情况下实现这一目标吗?这是什么最好的做法?
怎么了,我试着在评论列表中添加新评论,评论被映射到Post类的列表.
这是我的代码.
Post.java
@Document
public class Post {
@Id
private String id;
@DBRef
private List<Comment> comments;
public void addComment(Comment comment) {
if (comments == null) {
comments = new ArrayList<>();
}
this.comments.add(comment);
}
// getters and setters....
}
Run Code Online (Sandbox Code Playgroud)
Comment.java
@Document
public class Comment {
@Id
private String id;
private String comment;
private int rating;
// getters and setters....
}
Run Code Online (Sandbox Code Playgroud)
的Test.class
@Test
public void savePostWithComments() {
Post post = postRepository.findAll().get(1);
Comment comment = new Comment();
comment.setComment("comment");
comment.setRating(5);
post.addComment(comment);
postRepository.save(post);
}
Run Code Online (Sandbox Code Playgroud)
测试因此错误而失败 …