Tag: spring data jpa

修改Spring Data REST投影背后生成的SQL查询

编辑:如何只在SELECT中保留所需的列以进行Spring Data Rest Projections? Spring Data Rest Projections适用于为生成的链接获取列的子集,但在后面生成的Query仍然包含其中的所有列。 如何创建预测,其中SQL查询只有SELECT中的那些列在Projection中

始终使用JPA @Id的原始对象包装器而不是原始类型?

我发现使用原始类型作为JPA的对象@Id与Spring Data JPA一起使用的问题。 我在父母方面与Cascade.ALL有父/子关系,而小孩有PK,同时也是父母的FK。 class Parent { @Id private long id; @OneToOne(mappedBy = “parent”, cascade = ALL) private Child child; } class Child { @Id @OneToOne private Parent parent; } 所以,当我跑: … Parent parent = new Parent(); Child child = new Child(parent); parent.setChild(child); em.persist(parent) … 一切正常。 但我使用Spring Data JPA来持久保存实体,所以我改为运行: parentRepository.save(parent); // instead of em.persist(parent); 而且这个失败了以下例外: […]

我的自定义存储库实现中的CrudRepository

我试图获得对我的存储库接口( UserRepository )的引用,该接口在我的自定义实现( UserRepositoryExtensionImpl )中扩展CrudRepository ,以便获得对Spring JPA提供的所有方法的访问。 Crud扩展: @Repository public interface UserRepository extends CrudRepository, UserRepositoryExtension { …any custom spring JPA methods… } 扩展接口: @Repository public interface UserRepositoryExtension { public T put(T entity); } 定制实施: public class UserRepositoryExtensionImpl implements UserRepositoryExtension { UserRepository userRepository; @Autowired public UserRepositoryExtensionImpl(UserRepository userRepository) { this.userRepository = userRepository; } @Override public User put(User […]

优化Spring-Data JPA查询

我正在寻找框架生成的查询的可能优化。 据我了解,该过程如下: 你可以将你的域对象声明为POJO并添加几个注释,如@ManyToOne , @ManyToOne , @ManyToOne等。 你声明你的存储库,例如每个接口 使用(2),您可以使用多种方法来描述查询:例如,根据Methodnames或@Query 如果我写一个像这样的查询: @Query(“select t from Order t LEFT join fetch t.orderPositions where t.id = ?1″) Page findById(Pageable pageable, String id); 自动生成SQL查询,其中订单的每一列都被解析,并且顺序地用于订单定位和依赖于obejcts / tables。 好像我写道: select * from order 因此,如果我需要来自几个连接对象的一些信息,那么查询可能会非常昂贵:而且更有趣的是非常无效。 我偶然发现了一个缓慢的查询,MySQL-explain告诉我,在生成的查询中,优化器无法使用索引,这很糟糕。 当然(我知道)我必须处理权衡, 生成的 SQL不像手动编写那样最优,并且具有编写较少样板代码的优势。 我的问题是:改善查询,查询执行的好策略是什么? 我自己想过一些选择: 1)是否可以为不同的目的定义几个“实体”,例如Order访问Order的完整特征,以​​及类似FilteredOrder的列数较少但没有Join-columns分辨率? 两者都会引用相同的表,但是一个将使用所有列而另一个只使用一些。 2)使用@Query(… native=”true”)选择我想要使用的所有列。 这样做的好处是,我不会将我的域对象加倍,并且会使用数百个Filtered -Objects丢弃我的代码库。 分页怎么样? 将@Query( …native=”true”)与@Query( …native=”true”)结合使用仍然是可能的(恐怕不行)。 3)最后但在我眼中“最差”/样板解决方案:使用JDBCTemplates并在较低级别执行操作。 还有其他选择,我还没想过? […]

在spring-data项目中使用@Version

我一直在研究一个带有spring-data的RESTful web服务。 几天前,发布了一个特殊的spring-data jpa REST框架。 现在我注意到在这个框架中使用@Version的能力。 此版本是由自己生成还是您需要手动执行此操作? 它可以自己使用@Version吗? (这样我就不必将任何内容更改为现有的存储库/域等…) 我是否需要做一些额外的配置来使用@Version?

删除不使用JpaRepository

我有一个spring 4应用程序,我试图从我的数据库中删除一个实体的实例。 我有以下实体: @Entity public class Token implements Serializable { @Id @SequenceGenerator(name = “seqToken”, sequenceName = “SEQ_TOKEN”, initialValue = 500, allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = “seqToken”) @Column(name = “TOKEN_ID”, nullable = false, precision = 19, scale = 0) private Long id; @NotNull @Column(name = “VALUE”, unique = true) private String value; @ManyToOne(fetch […]

Spring Data JPA Projection从DB中选择了字段

我正在测试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; } 问题1: 对于以下预测: interface PersonLimited { String getFirstName(); AddressLimited getAddress(); } interface AddressLimited { […]

如何在spring-boot中设置hibernate.format_sql?

我正在使用spring-boot自动配置进行数据库注入,并定义了属性: spring.jpa.database=POSTGRESQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update 但是如何设置hibernate.format_sql=true ? 弹簧靴不支持吗?

如何防止Hibernate删除JSONpost中不存在的子对象?

我有一个有孩子的JPA Property实体(多个Rate和多个Reservation )。 在我的JavaScript应用程序中,我通过REST {property:{rates:[…], reservations[…]}提取JSON 。 费率和预订非常详细,因此当我发布属性更新(如更改名称)时,我会从JSON POST有效负载中删除费率和预留。 我希望Hibernate会简单地忽略丢失的密钥,但是唉,它正在删除所有保存的孩子。 如果不存在,我如何指定Hibernate忽略它们? @Entity public class Property { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; String name; @OneToMany(mappedBy = “property”, cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) @JsonManagedReference private Set rates = new HashSet(); @OneToMany(mappedBy = “property”, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = […]

弹簧数据jpa存储库注入失败,在Spring启动时使用@Autowired

春季启动入门课程 package com.test; @SpringBootApplication(exclude={ DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}) public class AssetManagementDigital2Application { public static void main(String[] args) { SpringApplication.run(AssetManagementDigital2Application.class, args); } } 控制器类 package com.test.assetmanagementdigital.controller; @RestController public class ShopController { @Autowired private ShopServiceImpl shopServiceImpl; @RequestMapping(value=”/shops”,method=RequestMethod.POST) public void shopDetails(Shop shop){ shopServiceImpl.addShopDetails(shop); } } 实体 package com.test.assetmanagementdigital.model; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name=”ShopDetails”) public class Shop { private String […]