spring-data-jpa

How do I use @IdClass with JPA Repository?

杀马特。学长 韩版系。学妹 提交于 2021-02-07 09:59:18
问题 I've been using a Spring JPA Repository interface to persist objects, and it works great! But how do I use it when the @Entity defines an @IdClass ? The class is not embedded, but the fields are part of the object. Is it expected I should create findBy methods that name all the composite fields? Or is there a way to create a findById and pass the PK class? 回答1: Nevermind - I found what I was missing. I was extending org.springframework.data.repository.Repository when what I really wanted was

Spring Data JPA - query with the date minus 2 days not working

大憨熊 提交于 2021-02-07 09:30:43
问题 I have this query that updates some prices higher that 2 days ago, but does not work @Transactional @Modifying @Query("update HotelDailyPrice hdp set hdp.price = (select avg (hp.price) " + "from HotelPrice hp where hp.id = ?1 and hp.updateDate > CURRENT_DATE - 2), hdp.day = ?2 ") void updateDailyAveragePrice (Hotel hotel, String dayDate); 回答1: Actually, JPA doesn't support time periods operations because not all databases support it. So you have following options: Calculate date

Spring Data JPA - query with the date minus 2 days not working

南楼画角 提交于 2021-02-07 09:30:41
问题 I have this query that updates some prices higher that 2 days ago, but does not work @Transactional @Modifying @Query("update HotelDailyPrice hdp set hdp.price = (select avg (hp.price) " + "from HotelPrice hp where hp.id = ?1 and hp.updateDate > CURRENT_DATE - 2), hdp.day = ?2 ") void updateDailyAveragePrice (Hotel hotel, String dayDate); 回答1: Actually, JPA doesn't support time periods operations because not all databases support it. So you have following options: Calculate date

Can't autowire repository from an external Jar into Spring Boot App

大憨熊 提交于 2021-02-07 06:52:17
问题 I have packaged my entire entities of the application and the repository interfaces into one jar. The repositories are written with @Repository annotation: @Repository public interface InternalUserRepository extends JpaRepository<InternalUser, Long>{ } I have included this jar file inside my spring boot application and trying to autowire the interface like this from a controller: @RestController public class AuthenticationController { @Autowired AuthenticationService authenticationService;

How to refer long query, written in external file, in Spring data jpa @Query

我是研究僧i 提交于 2021-02-06 11:05:10
问题 I want to write query (properties or yaml) in external file to load database. - This is long query and does not look good to eye when placed inside @Query("long query") in XXXRepository class. Is there way to write this query in an external file (properties, yaml, xml or json) and call that file in @Query() in spring data jpa? 回答1: You can use named queries, where the queries have to be defined in a file called META-INF/jpa-named-queries.properties . See the spring example: User

How to refer long query, written in external file, in Spring data jpa @Query

╄→尐↘猪︶ㄣ 提交于 2021-02-06 11:04:55
问题 I want to write query (properties or yaml) in external file to load database. - This is long query and does not look good to eye when placed inside @Query("long query") in XXXRepository class. Is there way to write this query in an external file (properties, yaml, xml or json) and call that file in @Query() in spring data jpa? 回答1: You can use named queries, where the queries have to be defined in a file called META-INF/jpa-named-queries.properties . See the spring example: User

Spring Data JPA/Hibernate handling associations

二次信任 提交于 2021-02-05 11:38:23
问题 I need based on parameter retrieve or not some associations from an entity. In the bellow example I need to get the records list only if a parameter is passed through my api. Can you recommend a way of achieving this using hibernate/spring data? I'm looking for the most clean and spring data-like approach. public class Customer { private UUID id; @OneToMany(mappedBy = "customer")
 private List<Record> records = new ArrayList<>(); } public class Record { private UUID id; @Column(name =

lateinit property vkUserRepository has not been initialized. Why?

六月ゝ 毕业季﹏ 提交于 2021-02-05 09:47:40
问题 I try "kotlin - spring boot 2 - jpa".I just started to study spring-boot. I have model, repository and app files. I`m use start.spring.io for starting.I’ve seen examples with web, but I don’t need a web. I have error during compilation. Why ? How can I fix this error? 2018-12-21 13:26:02.732 INFO 28188 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect 2018-12-21 13:26:02.923 INFO 28188 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl

`Type cannot be null` exception when trying to run out Stored Procedure using Spring Data JPA

风格不统一 提交于 2021-02-05 09:40:52
问题 I am trying to invoke a Stored Procedure whose signature looks like the following: CREATE OR REPLACE PROCEDURE FIND_FIRST_BOOKMARK_GT(bookmark IN NUMBER, cur OUT SYS_REFCURSOR) I am using Spring-Data JPA and have tried a number of different variants, but all of them follow more or less the following pattern. My entity model is decorated as follows: @NamedStoredProcedureQuery(name = "Response.findFirstBookmarkGreaterThan", procedureName = "FIND_FIRST_BOOKMARK_GT", resultClasses = Response

ManyToMany relationship leads to StackOverFlow error

孤街醉人 提交于 2021-02-05 07:45:09
问题 I have an entity called User which has set of roles. I also have a Role entity which has set of users.( This is just a practice application for learning purposes.) public class User { private String firstName; private String lastName; @ManyToMany @JoinTable(name="user_role", joinColumns={@JoinColumn(name="user_id")}, inverseJoinColumns={@JoinColumn(name="role_id")}) private Set<Role> roles; //getters setters } public class Role { private String name; @ManyToMany(mappedBy="roles") private Set