想象一下以下型号:
雇员:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "employee_project", joinColumns = @JoinColumn(name = "Emp_Id"), inverseJoinColumns = @JoinColumn(name = "Proj_id"))
private Set<Project> projects = new HashSet<Project>();
Run Code Online (Sandbox Code Playgroud)
项目:
@ManyToMany(mappedBy = "projects")
private Set<Employee> employees = new HashSet<Employee>();
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建一个引用现有项目并尝试保留该员工的新员工,则会收到错误消息:
detached entity passed to persist: Project
Run Code Online (Sandbox Code Playgroud)
我按如下方式创建员工:
public void createNewEmployee(EmployeeDTO empDTO) {
Employee emp = new Employee();
// add stuff from DTO, including projects
repository.saveAndFlush(emp); // FAILS
}
Run Code Online (Sandbox Code Playgroud)
我更新现有的这样的:
public void updateEmployee(EmployeeDTO empDTO) {
Employee emp = repository.findOne(empDTO.getId());
// set stuff from DTO, including projects …Run Code Online (Sandbox Code Playgroud) 如何在Spring Data REST 资源的根列表中公开外部资源(不通过存储库管理)?我在Restbucks中按照模式定义了一个控制器
我spring-data昨天开始使用项目,并试图将测试添加到我创建的存储库中.
项目结构看起来像
persistence/pom.xml
src/main/java/
ApplicationConfig
BaseRepository
Network
src/main/test/BaseRepositoryTest
Run Code Online (Sandbox Code Playgroud)
的ApplicationConfig模样
@Configuration
@ComponentScan
@EnableJpaRepositories
public class ApplicationConfig {
@Bean
public DataSource dataSource() {
final EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
embeddedDatabaseBuilder.setType(EmbeddedDatabaseType.H2);
return embeddedDatabaseBuilder.build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.H2);
jpaVendorAdapter.setGenerateDdl(true);
final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
localContainerEntityManagerFactoryBean.setPackagesToScan(getClass().getPackage().getName());
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
return localContainerEntityManagerFactoryBean;
}
}
Run Code Online (Sandbox Code Playgroud)
的BaseRepository模样
import org.springframework.data.repository.Repository;
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
List<T> findAll();
}
Run Code Online (Sandbox Code Playgroud)
这 …
我正在尝试使用弹簧数据的新功能,投影来获取部分实体(NetworkSimple)的页面
我已经检查了文档,如果我只是请求:
Collection<NetworkSimple> findAllProjectedBy();
Run Code Online (Sandbox Code Playgroud)
它有效,但如果我使用可分页:
Page<NetworkSimple> findAllProjectedBy(Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误:
org.hibernate.jpa.criteria.expression.function.AggregationFunction$COUNT cannot be cast to org.hibernate.jpa.criteria.expression.CompoundSelectionImpl
Run Code Online (Sandbox Code Playgroud)
任何人已经使用过这个吗?
我的NetworkSimple类如下:
public interface NetworkSimple {
Long getId();
String getNetworkName();
Boolean getIsActive();
}
Run Code Online (Sandbox Code Playgroud) 我有这个数据模型
public class CustomerModel{
@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime membershipDate;
//Other properties and getters
}
Run Code Online (Sandbox Code Playgroud)
以下回购
public interface CustomerRepo extends Repository<CustomerModel, Long>{}
Run Code Online (Sandbox Code Playgroud)
我想做的是.检索给定日期的所有用户,例如(2013年8月1日加入的成员)但问题是我的数据库上的membershipDate有时间.如何忽略时间并检索给定日期的所有用户?
我的文档结构如下:
{
_id: "A",
groups:[{
groupId: "someId",
groupName: "someName",
params: {
type1: ["a", "b"],
type2: ["c", d]
}
}],
config: {
person: {}
dataDetails: {
dataTypeDetails: {},
dataList: ["dt1", "dt2"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的Spring Data MongoDB模型类型如下所示:
// Imports etc.
@Document
public class Entity {
@Id
private String _id;
private List<Group> groups;
private Config config;
// setters and getters
public class Group {
private String groupId;
private String groupName;
private ParamData params;
// setter and getters
}
public class ParamData { …Run Code Online (Sandbox Code Playgroud) 对于Java枚举类型,我了解到MongoDB有两种解决方案:序列化和使用Jackson的ObjectMapper.MongoRepository可以使用这些方法中的任何一种使用枚举数据类型,或者我必须编写自定义存储库吗?
单独使用 Spring Data REST 存储库并围绕它实现“服务”模式(即ItemService,ItemServiceImpl等等)有什么区别?
乍一看,功能或多或少相同,不同之处在于服务方法允许更好的定制,但它也会产生大量的样板代码(实现和控制器)。这是使用这两种方法的示例(外观Payment和CreditCard实体) - Oliver Drotbohm 的 RESTBucks。
那里的支付抽象使用使用的“服务”模式(PaymentService、PaymentImpl 然后 PaymentController 和 web 文件夹中的所有方法),而订单直接通过 Spring Data REST 公开。
我正在使用spring-data-elasticsearch和elasticsearch来查询文档.我想对嵌套文档进行嵌套查询.
我在java中有这个:
@Document(indexName = "as", type = "a", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
class A {
@Id
private String Id;
@Field(type = String, index = analyzed, store = true)
private String field1;
// ... Many more Fields.
@NestedField(type = FieldType.Object, index = analyzed, store = true, dotSuffix = "accounts")
private List<B> bs;
// ... getters and setters
}
Run Code Online (Sandbox Code Playgroud)
和
class B { // some normal pojo }
Run Code Online (Sandbox Code Playgroud)
当我让spring-data进行映射时,我得到:
"a": {
"properties": {
"bs": …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用spring-boot-starter-data-rest使用Spring Boot构建RESTful API.有一些实体:帐户,交易,类别和用户 - 只是通常的东西.
当我通过默认生成的API 检索http:// localhost:8080/transactions中的对象时,一切顺利,我得到一个包含所有事务的列表作为JSON对象,如下所示:
{
"amount": -4.81,
"date": "2014-06-17T21:18:00.000+0000",
"description": "Pizza",
"_links": {
"self": {
"href": "http://localhost:8080/transactions/5"
},
"category": {
"href": "http://localhost:8080/transactions/5/category"
},
"account": {
"href": "http://localhost:8080/transactions/5/account"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但现在的目标是仅检索该URL下的最新事务,因为我不想序列化整个数据库表.所以我写了一个控制器:
@Controller
public class TransactionController {
private final TransactionRepository transactionRepository;
@Autowired
public TransactionController(TransactionRepository transactionRepository) {
this.transactionRepository = transactionRepository;
}
// return the 5 latest transactions
@RequestMapping(value = "/transactions", method = RequestMethod.GET)
public @ResponseBody List<Transaction> getLastTransactions() {
return transactionRepository.findAll(new PageRequest(0, 5, new Sort(new Sort.Order(Sort.Direction.DESC, "date")))).getContent(); …Run Code Online (Sandbox Code Playgroud) spring spring-mvc spring-data-rest spring-hateoas spring-boot