这个问题的标题是相当矛盾的,因为我试图在非关系数据库中实现关系... :)
但我的意思是如何定义使用MongoDB的应用程序模型类中的实体之间的关联.
使用JPA我经常使用@ManyToMany或@OneToMany注释来定义对象之间的关系.Spring Data MongoDB中有类似的东西吗?
研究MongoDB我意识到这种关联有两种可能的方法:参考和嵌入数据.
Spring Data使用哪一个?是否可以配置关联模式?
我正在处理一个奇怪的问题,我正在进行集成测试,调用我的控制器从数据库中获取一个不存在的对象.
public Optional<T> get(Long id) {
try {
return Optional.ofNullable(repository.getOne(id));
} catch(EntityNotFoundException e) {
return Optional.empty();
}
}
Run Code Online (Sandbox Code Playgroud)
什么时候getOne(…)找不到什么东西,我一直在期待,EntityNotFoundException但实际上什么都没有.如果我检查我的结果,我可以看到我有一个空实体,其中有一个处理程序链接"扔EntityNotFoundException"但我们没有进入catch并且我返回一个可选的这个奇怪的实体.
我无法理解这种行为.
我们完全按照Spring Data文档中的说明实现了我们的存储库.一切都很好,直到我们从STS 2.9升级到STS 3.1.所有使这些错误消失的尝试都失败了,在某些情况下它们甚至没有意义!它们与界面或使用的实体中的任何属性都不匹配!
这是一个例子:
public interface CreditNotesRepository extends JpaRepository<CreditNotes, Long> {
CreditNotes findCurrentCreditNotes(Long shipmentDetailId);
}
Run Code Online (Sandbox Code Playgroud)
这findCurrentCreditNotes是我们实体中的命名查询.这段代码执行得很好.
@NamedQueries({
@NamedQuery(name = "CreditNotes.getCount", query = "SELECT COUNT(f) FROM CreditNotes f"),
@NamedQuery(name = "CreditNotes.findCurrentCreditNotes", query =
"SELECT creditNotes FROM CreditNotes creditNotes"
+ " WHERE creditNotes.shipmentDetail.shipmentDetailId = ?1 "
+ " AND creditNotes.notesSeqNumber = (SELECT max(creditNotes2.notesSeqNumber) FROM CreditNotes creditNotes2"
+ " WHERE creditNotes.shipmentDetail.shipmentDetailId = creditNotes2.shipmentDetail.shipmentDetailId)")
})
Run Code Online (Sandbox Code Playgroud)
我们得到的错误:
Invalid derived query! No property find found for type ca.cole.freight.model.CreditNotes
Run Code Online (Sandbox Code Playgroud)
虽然这只是一个标志(不影响编译),但令人讨厌并且令人困惑.任何人都可以对此有所了解吗?并向我解释,就像我6岁!;)
spring spring-mvc sts-springsourcetoolsuite spring-data spring-data-jpa
我在spring webmvc项目中使用spring-data-jpa.我在我的一个实体的存储库上使用查询创建时遇到了问题.您可以在下面看到我的实体,我的存储库和例外.
我的实体,
@Entity
@Table(schema = "mainschema")
@XmlRootElement
public class Municipalperson implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "municipal_id", nullable = false)
private Integer municipal_id;
@Basic(optional = false)
@Column(nullable = false, length = 60)
private String firstname;
public Municipalperson(Integer id, Integer municipal_id, String firstname) {
this.id = id;
this.municipal_id = municipal_id;
this.firstname = firstname;
}
public Integer …Run Code Online (Sandbox Code Playgroud) 我正在测试Spring Data 1.10.4.RELEASE,遵循Spring Data Docs中的示例http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
我注意到一些问题,我有两个问题.
首先让我假设我有这两个实体:
@Entity
public class Person {
@Id @GeneratedValue
private Long id;
private String firstName, lastName;
@OneToOne
private Address address;
}
@Entity
public class Address {
@Id @GeneratedValue
private Long id;
private String street, state, country;
}
Run Code Online (Sandbox Code Playgroud)
对于以下预测:
interface PersonLimited {
String getFirstName();
AddressLimited getAddress();
}
interface AddressLimited {
String getCountry();
}
Run Code Online (Sandbox Code Playgroud)
当我运行findPersonByFirstNameProjectedForLimitedData时
interface PersonRepository extends CrudRepository<Person, Long> {
@Query("select p from Person p where p.firstName = ?1")
PersonLimited findPersonByFirstNameProjectedForLimitedData(String firstName);
} …Run Code Online (Sandbox Code Playgroud) 我运行主类时遇到错误.
错误:
Action:
Consider defining a bean of type 'seconds47.service.TopicService' in your configuration.
Description:
Field topicService in seconds47.restAPI.topics required a bean of type 'seconds47.service.TopicService' that could not be found
Run Code Online (Sandbox Code Playgroud)
TopicService接口:
public interface TopicService {
TopicBean findById(long id);
TopicBean findByName(String name);
void saveTopic(TopicBean topicBean);
void updateTopic(TopicBean topicBean);
void deleteTopicById(long id);
List<TopicBean> findAllTopics();
void deleteAllTopics();
public boolean isTopicExist(TopicBean topicBean);
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@RestController
public class topics {
@Autowired
private TopicService topicService;
@RequestMapping(path = "/new_topic2", method = RequestMethod.GET)
public void new_topic() throws Exception { …Run Code Online (Sandbox Code Playgroud) 这可能不是最好的问题,但我注意到Spring引导有2个Spring JPA.他们有什么不同.目前我正在尝试与hibernate一起设置SpringBoot 1.5.3项目.我记得我之前使用spring-boot-starter-data-jpa设置了Springboot和JPA.
我见过的大多数在线示例以及starter.spring.io都为spring jpa提供了以下依赖项.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是在一个现有的项目中,我遇到了spring-data-jpa
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.4.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
做一些谷歌没有给我,如果他们是不同的.
在我之前的所有项目中,我添加JPA虽然是JPA 2.1/Hibernate,但这就是为什么我有点不确定在我的新SpringBoot应用程序中使用哪两个.
这将是我第一次将Spring连接到Redis.jedis连接工厂的文档:http://www.baeldung.com/spring-data-redis-tutorial
提供以下代码:
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory
= new JedisConnectionFactory();
jedisConFactory.setHostName("localhost");
jedisConFactory.setPort(6379);
return jedisConFactory;
}
Run Code Online (Sandbox Code Playgroud)
看起来很棒,但我的IDE告诉我setHostName和setPort方法已被弃用(即使我使用的是教程中的版本).
我想知道是否有人使用非弃用API调用的简单"获取弹簧数据连接到redis"示例?
我有一个使用spring-data-rest的项目,并且有一个仅使用Spring Data的依赖项目.这两个项目都有spring数据存储库并用于@EnableJpaRepositories实现其存储库接口,但我只想在父项目中导出存储库.
这是我的问题:是否有一些方法可以将Spring Data REST配置为仅公开父项目中资源的休息端点,而不必显式注释依赖项目中的每个存储库@RepositoryRestResource(exported = false)?
如果我只能通过@RepositoryRestResource禁用它来实现这一点,更糟糕的是,没有其他具有不同用例的项目将能够为这些存储库启用REST端点,我的依赖项目将不得不包括仅用于...的Spring Data REST.
我有一个像这样的对象页面:
Page<Video> videos = videoRepository.findAllVideos(new PageRequest(1, 50));
Run Code Online (Sandbox Code Playgroud)
如何在不迭代我的页面的情况下将其转换为视频obj列表?
spring-data ×10
java ×8
spring ×5
jpa ×2
spring-boot ×2
jedis ×1
mongodb ×1
projection ×1
redis ×1
repository ×1
spring-mvc ×1