hibernate

How to map sql native query result into DTO in spring jpa repository?

穿精又带淫゛_ 提交于 2021-02-17 04:46:33
问题 Hi what I am trying to achieve is to get SQL native query result map into my DTO in java spring jpa repository, how do I do this properly? I try several code, but it does not work, here is what I tried: First try : @Repository public interface StockRepository extends RevisionRepository<Stock, Long, Integer>, JpaRepository<Stock, Long> { @Query(value = "SELECT stock_akhir.product_id AS productId, stock_akhir.product_code AS productCode, SUM(stock_akhir.qty) as stockAkhir " + "FROM book_stock

hibernate and mappedBy: Is it possible to automatically set foreign key without setting bidirectional relationship among objects?

我只是一个虾纸丫 提交于 2021-02-17 03:47:29
问题 Welcome, I have 2 classes: Conversation and Question. One Conversation have many questions. Conversation.java: package com.jcg.jpa.mappedBy; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax

hibernate bug:ArrayIndexOutOfBoundsException on...

孤街浪徒 提交于 2021-02-17 02:01:13
昨天给程序加上了2级缓存,今天打开查询统计页面,就报错: ava.lang.ArrayIndexOutOfBoundsException: 0 at org.hibernate.type.TypeHelper.disassemble(TypeHelper.java:146) at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:106) at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2434) at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2321) 才开始以为是java 版本换了的原因,最后直接搜索错误找到了这样个bug: ArrayIndexOutOfBoundsException on enabling cache for a Native SQL query 高版本的已经修订了这个错误,但换hibernate 版本肯定又一堆问题,所以在使用native sql query 的时候加上: q.setCacheable(false); 这样 就OK 了,2级缓存 语句缓存同意还是开启的,本地sql 查询禁用2级查询缓存就可以了 来源

Getting all children and subchildren from parent JPA

感情迁移 提交于 2021-02-16 20:55:09
问题 I have a table TABLE that can have parents of the same type. In Java the child can reach the parent but the parent doesn't have a list of children. In MySQL I was able to create the following query that gives me the children and subchildren of the parent, but I'm unable to translate this into JPA Java. How can I translate this query: SELECT * FROM TABLE AS T1 INNER JOIN (SELECT id FROM TABLE WHERE TABLE.parentId = 966) AS T2 ON T2.id = T1.parentId OR T1.parentId = 966 GROUP BY T1.id Into java

Stackoverflow error when saving an Object's toString value - Java/Hibernate/Spring

ぐ巨炮叔叔 提交于 2021-02-16 20:07:37
问题 I have the following sample entities: Institution @Data @Entity @NoArgsConstructor @EntityListeners(InstitutionAuditListener.class) public class Institution extends Auditable<String> { @OneToMany(mappedBy = "institution", cascade = CascadeType.ALL) @JsonManagedReference private List<RegisteredProgram> registeredPrograms; private String name; } RegisteredProgram @Data @NoArgsConstructor @Entity @EntityListeners(RegisteredProgramAuditListener.class) public class RegisteredProgram extends

Stackoverflow error when saving an Object's toString value - Java/Hibernate/Spring

谁说胖子不能爱 提交于 2021-02-16 20:07:17
问题 I have the following sample entities: Institution @Data @Entity @NoArgsConstructor @EntityListeners(InstitutionAuditListener.class) public class Institution extends Auditable<String> { @OneToMany(mappedBy = "institution", cascade = CascadeType.ALL) @JsonManagedReference private List<RegisteredProgram> registeredPrograms; private String name; } RegisteredProgram @Data @NoArgsConstructor @Entity @EntityListeners(RegisteredProgramAuditListener.class) public class RegisteredProgram extends

Hibernate @Version annotation

[亡魂溺海] 提交于 2021-02-16 18:12:08
问题 What is the relation between hibernate @version and ManyToOne Mapping. Assume that i am having two tables Department and Employee. Here is Deparment is the master table and Employee in the detail table. In the Employee table, departmentID is reference as foreign key. Here is my classes Public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long ID; @Version private Long version; //Getters and Setters } public class Employee { @Id @GeneratedValue(strategy =

Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: Implementing class

穿精又带淫゛_ 提交于 2021-02-16 16:31:42
问题 error message: SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [config/spring/Hibernate-ctx.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: Implementing class at org.springframework.beans.factory.support

JpaRepository won't save data

泄露秘密 提交于 2021-02-16 11:19:13
问题 I use JpaRepository to save data, but the hibernate.show_sql shows "select" and won't save data. Following is my service: @Autowired private UserRepository userRepository; @PostConstruct public void init() { User admin = new User(); admin.setDisplayName("admin"); admin.setEmailAddress("admin@admin"); admin.setPassword("admin___"); admin.setRegisteredAt(new Date()); admin.setLastAccessAt(new Date()); admin.setUuid(UUID.randomUUID().toString()); try { System.out.println("before save");

Mybatis 你踩过坑吗?

余生颓废 提交于 2021-02-16 10:05:39
作者:似水的流年 yq.aliyun.com/roundtable/49835 大多数开发者应该都使用过Hibernate或者Mybatis的框架,或多或少都踩过一些坑! 如在MyBatis/Ibatis中#和$的区别,#方式能够很大程度防止sql注入,$方式无法防止Sql注入。所以,老司机 对新手说,最好用#。简单的说#{}是经过预编译的,是安全的,而是未经过预编译的,仅仅是取变量的值,是非安全的,存在sql注入。有些特例是需要关注的,有的时候需要用 解决解决一些实际问题。 如在执行sql语句时你有时并不希望让变量进行处理,而是直接赋值执行,这时就要用到(${a})了,在使用时还要这样赋值 @Param(value="a") String a 如日期问题: 可能会遇到日期格式的时间段问题,当数据库的时间为DATE类型时,MyBatis的jdbcType应该使用DATE jdbcType=DATE, 而不是使用 jdbcType=TIMESTAMP 如在使用resultMap的时候,要把ID写在第一行,否则的话,就会报错。 又如最近在做的项目,遇到myBatis的大坑,Mybatis一直报异常: Java.lang.ArrayIndexOutOfBoundsException, 于是开始代码查错,代码中有存储过程,然后开发使用ROOT用户执行SQL跑出来的数据结果集是正常的