0%

Hibernate入门

hibernate配置文件

hibernate配置文件

hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据源配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>

<!-- 数据库方言 MySQL -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 连接池 C3P0 -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>

<!-- 格式化输出生成的SQL语句 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>

<!-- hibernate根据映射关系自动建表
默认: 不会创建表
create: 没有表就创建,有表就删除重建。
create-drop: 没有表就创建,有表就删除重建,使用完自动删表。
update: 没有表就创建表,否则使用现有的表。
validate: 校验实体关系映射文件和数据表是否对应,不对应则报错
-->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 加载映射文件,这里写的时路径而不是包名,以'/'分隔 -->
<mapping resource="hbm/WFProcessProperty.hbm.xml" />

</session-factory>
</hibernate-configuration>

映射关系文件

映射关系文件.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- 这里写包名,下面就可以直接写类名 -->
<hibernate-mapping package="pojo">
<class name="Course" table="course">
<!-- 主键id需要单独配置-->
<id name="id" type="long" column="id">
<generator class="assigned" />
</id>
<property name="name" column="name" type="java.lang.String" not-null="false" />
<list name="students">
<key column="sid"></key>
<!-- 排序index -->
<list-index column="sid"></list-index>
<!-- list中的对象关系映射 -->
<many-to-many class="Student"></many-to-many>
</list>
</class>
</hibernate-mapping>

hibernate-mapping标签的一些属性:

image-20220116213025846

class标签的一些属性:

image-20220116213629173

主键映射的方式:

  • assigned:外部程序对id赋值
  • native:由数据库对id赋值
  • identity:采用数据库提供的主键生成机制
  • increment:采用hibernate提供的主键生成机制
  • sequence:采用数据库提供的sequence机制生成主键
  • foreign:使用外部表的字段作为主键

property标签常用属性:

  • update:字段是否可以修改,默认为true
  • insert:字段是否可以添加,默认为true
  • lazy:是否采用延迟加载策略,默认为true

集合类的常用属性:

  • inverse:本表是否参与维护关系,默认为true,为false时会将维护权转让给对方类
  • cascade:级联操作,当对本表操作时是否对集合中的类的对应表进行操作

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class CommonTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;

@Before
public void init() {
//默认会自动去加载resourcs路径下hibernate.cfg.xml文件中的配置
//可以在configure()方法中指明其他路径
Configuration configure = new Configuration().configure();
sessionFactory = configure.buildSessionFactory();
session = sessionFactory.openSession();
//事务
transaction = session.beginTransaction();
}

@After
public void destroy() {
//提交事务
transaction.commit();
session.close();
sessionFactory.close();
}

@Test
public void hibernateTest() {
Student student = session.get(Student.class, 1L);
System.out.println("修改前:");
System.out.println(student);

student.setName("李四");
session.update(student);

student = session.get(Student.class, 1L);
System.out.println("修改后:");
System.out.println(student);
}
}

HQL

HQL:Hibernate Query Language,是一种面向对象的查询语言,可以完成查询、修改、删除操作,但不能完成插入操作

HQL是在Hibernate中作用的中间层语言,查询过程:java -> HQL -> SQL

  • 查询语句

    1
    2
    3
    4
    5
    6
    7
    8
    //最简单的hql,默认select *
    String hql = "from Student";
    Query query = session.createQuery(hql);
    //简单分页
    query.setFirstResult(0);
    query.setMaxResults(1);
    List<Student> list = query.list();
    list.forEach(System.out::println);

    只需要选取单独字段可以完全拼写

    1
    String hql = "select name from Student";

    也可以直接追加where条件

    1
    2
    3
    String hql = "from Student where id = 2896391706445565017L";
    Query query = session.createQuery(hql);
    Student student = (Student) query.uniqueResult();

    像preparedStatement那样使用占位符

    1
    2
    3
    String hql = "from WFProcessProperty where id = :id";
    Query query = session.createQuery(hql);
    query.setLong("id",2896391706445565017L);

hibernate使用小坑

无法检测到.hbm.xml文件

注意:这个文件和mybatis的mapper文件一样,如果需要放在java源码目录下,需要在pom.xml中进行配置

1
2
3
4
5
6
7
8
9
10
11
12
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

输出对象导致stackoverflow

如果两个实体对象之间有循环依赖关系,如

1
2
3
4
5
6
@Data
public class Student {
private long id;
private String name;
private List<Course> courses;
}
1
2
3
4
5
6
@Data
public class Course {
private long id;
private String name;
private List<Student> students;
}

我们在使用lombok时可能不太注意,@Data注解为我们自动生成了toString()方法,Course类的toString()方法示例如下:

1
2
3
4
5
6
7
public String toString() {
return "Course{" +
"id=" + id +
", name='" + name + '\'' +
", students=" + students +
'}';
}

这里的返回拼接的students中又会去调用Student类的toString()方法,而Student类的toString()方法又会去调用Course类的toString()。。。进而出现无限的循环

解决办法:

手动实现任意一方的toString方法,取消对另一方的调用