hibernate

Hibernate performs twice the same query if a bidirectional relationship is defined

余生长醉 提交于 2021-01-03 03:29:11
问题 I'm using Hibernate 4.3.8.Final and Oracle 11g database. I defined a very simple bidirectional relationship between two entities, named Parent and Child, as follows (getters and setters are omitted): @Entity public class Parent { @Id private Long id; @OneToOne(mappedBy="parent",fetch=FetchType.LAZY) private Child child; } @Entity public class Child { @Id private Long id; @OneToOne(fetch=FetchType.EAGER) @JoinColumn(name="PARENT_ID") private Parent parent; } The SQL code which generates the

Spring Boot :使用 validation 验证参数

大兔子大兔子 提交于 2021-01-02 22:59:06
一、简介   开发web项目有时候我们需要对controller层传过来的参数进行一些基本的校验,比如非空,非null,整数值的范围,字符串的个数,日期,邮箱等等。最常见的就是我们直接写代码校验,这样以后比较繁琐,而且不够灵活。   Bean Validation 1.0(JSR-303)是一个校验规范,在Spring Boot项目由于自带了Hibernate validator 5( http://hibernate.org/validator/ )实现,所以我们可以非常方便的使用这个特性 二、关键使用 1、添加包   hibernate-validator <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator --> < dependency > < groupId > org.hibernate.validator </ groupId > < artifactId > hibernate-validator </ artifactId > < version > 6.0.2.Final </ version > </ dependency >   或者添加spring-boot-starter-validation <!-- https:/

Mybatis面试题

可紊 提交于 2021-01-02 14:04:14
面试题示例 1、JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? 1)数据库链接创建、释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题。 解决:在 SqlMapConfig.xml中配置数据链接池,使用连接池管理数据库链接。 2)Sql语句写在代码中造成代码不易维护,实际应用sql变化的可能较大,sql变动需要改变java代码。 解决:将 Sql语句配置在XXXXmapper.xml文件中与java代码分离。 3)向sql语句传参数麻烦,因为sql语句的where条件不一定,可能多也可能少,占位符需要和参数一一对应。 解决: Mybatis自动将java对象映射至sql语句。 4)对结果集解析麻烦,sql变化导致解析代码变化,且解析前需要遍历,如果能将数据库记录封装成pojo对象解析比较方便。 解决: Mybatis自动将sql执行结果映射至java对象。 2、MyBatis编程步骤是什么样的? 1)创建SqlSessionFactory 2)通过SqlSessionFactory创建SqlSession 3)通过sqlsession执行数据库操作 4)调用session.commit()提交事务 5)调用session.close()关闭会话 3、 MyBatis与Hibernate有哪些不同? 1)Mybatis和hibernate不同

Mybatis面试题整理(超详细)

放肆的年华 提交于 2021-01-02 13:05:05
1、什么是Mybatis? (1)Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。程序员直接编写原生态sql,可以严格控制sql执行性能,灵活度高。 (2)MyBatis 可以使用 XML 或注解来配置和映射原生信息,将 POJO映射成数据库中的记录,避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 (3)通过xml 文件或注解的方式将要执行的各种 statement 配置起来,并通过java对象和 statement中sql的动态参数进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射为java对象并返回。(从执行sql到返回result的过程)。 2、Mybaits的优点: (1)基于SQL语句编程,相当灵活,不会对应用程序或者数据库的现有设计造成任何影响,SQL写在XML里,解除sql与程序代码的耦合,便于统一管理;提供XML标签,支持编写动态SQL语句,并可重用。 (2)与JDBC相比,减少了50%以上的代码量,消除了JDBC大量冗余的代码,不需要手动开关连接; (3)很好的与各种数据库兼容(因为MyBatis使用JDBC来连接数据库,所以只要JDBC支持的数据库MyBatis都支持)。 (4)

Spring data repository sends null as bytea to PostgreSQL database

守給你的承諾、 提交于 2021-01-02 05:11:25
问题 After switching from MySQL to PostgreSQL I found out that my SQL query (@Query in spring data repository interface) does not work anymore. The issue is caused by null value being sent as bytea and I'm getting following exception: Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Repository with @Query: public interface WineRepository extends

Connection Leak in Hibernate application

十年热恋 提交于 2021-01-01 17:38:49
问题 I am working in java enterprise application. The application has hibernate framework for backend. Due to recent changes in application some code consumes all JDBC connection pools from weblogic server. The application connection was property handled in code, for each thread we are create each session using threadlocal class. So there was no issues with creating connection. The application was live more than 5 years. We are suspecting the recent code changes causes this major issue. Finally we

Connection Leak in Hibernate application

a 夏天 提交于 2021-01-01 17:36:16
问题 I am working in java enterprise application. The application has hibernate framework for backend. Due to recent changes in application some code consumes all JDBC connection pools from weblogic server. The application connection was property handled in code, for each thread we are create each session using threadlocal class. So there was no issues with creating connection. The application was live more than 5 years. We are suspecting the recent code changes causes this major issue. Finally we

Connection Leak in Hibernate application

▼魔方 西西 提交于 2021-01-01 17:33:38
问题 I am working in java enterprise application. The application has hibernate framework for backend. Due to recent changes in application some code consumes all JDBC connection pools from weblogic server. The application connection was property handled in code, for each thread we are create each session using threadlocal class. So there was no issues with creating connection. The application was live more than 5 years. We are suspecting the recent code changes causes this major issue. Finally we

Connection Leak in Hibernate application

六月ゝ 毕业季﹏ 提交于 2021-01-01 17:29:50
问题 I am working in java enterprise application. The application has hibernate framework for backend. Due to recent changes in application some code consumes all JDBC connection pools from weblogic server. The application connection was property handled in code, for each thread we are create each session using threadlocal class. So there was no issues with creating connection. The application was live more than 5 years. We are suspecting the recent code changes causes this major issue. Finally we

Hibernate custom query containing GROUPBY and HAVING

时间秒杀一切 提交于 2021-01-01 09:55:32
问题 So I have two entities with a one-to-many relationship. A form_collection contains multiple form . The form contains a column version which keeps track of the most current form. form +----+-----------------+---------+--------------------+ | id | description_key | version | form_collection_id | +----+-----------------+---------+--------------------+ | 1 | desc1 | 1 | 1 | +----+-----------------+---------+--------------------+ | 2 | desc1 | 2 | 1 | +----+-----------------+---------+------------