Skip to content

Commit 38ae798

Browse files
authored
JAVA-41258: Moving some article links on Github - hibernate-queries (#18068)
1 parent 137ddd3 commit 38ae798

File tree

27 files changed

+192
-25
lines changed

27 files changed

+192
-25
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
## Relevant Article:
22
- [ON CONFLICT Clause for Hibernate Insert Queries](https://www.baeldung.com/hibernate-insert-query-on-conflict-clause)
3+
- [Get All Data from a Table with Hibernate](https://www.baeldung.com/hibernate-select-all)
4+
- [Hibernate Query Plan Cache](https://www.baeldung.com/hibernate-query-plan-cache)
5+
- [Hibernate’s addScalar() Method](https://www.baeldung.com/hibernate-addscalar)
6+
- [Distinct Queries in HQL](https://www.baeldung.com/java-hql-distinct)
7+
- [Database Keywords as Columns in Hibernate Entities](https://www.baeldung.com/java-hibernate-db-keywords-as-columns)
8+
- [Get List of Entity From Database in Hibernate](https://www.baeldung.com/java-hibernate-fetch-entity-list)

persistence-modules/hibernate-queries-2/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
</parent>
1515

1616
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-context</artifactId>
20+
<version>${org.springframework.version}</version>
21+
</dependency>
1722
<dependency>
1823
<groupId>org.hibernate.orm</groupId>
1924
<artifactId>hibernate-core</artifactId>
@@ -34,6 +39,22 @@
3439
<artifactId>mariaDB4j</artifactId>
3540
<version>${mariaDB4j.version}</version>
3641
</dependency>
42+
<dependency>
43+
<groupId>org.openjdk.jmh</groupId>
44+
<artifactId>jmh-generator-annprocess</artifactId>
45+
<version>${jmh-generator.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.testcontainers</groupId>
49+
<artifactId>mysql</artifactId>
50+
<version>${testcontainers.mysql.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.mysql</groupId>
55+
<artifactId>mysql-connector-j</artifactId>
56+
<version>${mysql.version}</version>
57+
</dependency>
3758
</dependencies>
3859

3960
<build>
@@ -50,8 +71,11 @@
5071
</build>
5172

5273
<properties>
74+
<org.springframework.version>6.0.6</org.springframework.version>
5375
<mariaDB4j.version>2.6.0</mariaDB4j.version>
5476
<h2.version>2.1.214</h2.version>
77+
<testcontainers.mysql.version>1.17.6</testcontainers.mysql.version>
78+
<mysql.version>8.2.0</mysql.version>
5579
</properties>
5680

5781
</project>

persistence-modules/hibernate-queries-2/src/main/java/com/baeldung/hibernate/HibernateUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.net.URL;
66
import java.util.Properties;
77

8+
import com.baeldung.hibernate.distinct.entities.Comment;
9+
import com.baeldung.hibernate.distinct.entities.Post;
10+
import com.baeldung.hibernate.entities.DeptEmployee;
811
import org.apache.commons.lang3.StringUtils;
912
import org.hibernate.SessionFactory;
1013
import org.hibernate.boot.Metadata;
@@ -37,6 +40,10 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
3740

3841
metadataSources.addPackage("com.baeldung.hibernate.pojo");
3942
metadataSources.addAnnotatedClass(Student.class);
43+
metadataSources.addAnnotatedClass(Comment.class);
44+
metadataSources.addAnnotatedClass(Post.class);
45+
metadataSources.addAnnotatedClass(DeptEmployee.class);
46+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
4047

4148
Metadata metadata = metadataSources.getMetadataBuilder()
4249
.build();
File renamed without changes.
File renamed without changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.hibernate.entities;
2+
3+
import java.util.List;
4+
5+
import jakarta.persistence.*;
6+
7+
@Entity
8+
public class Department {
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
11+
private long id;
12+
13+
private String name;
14+
15+
@OneToMany(mappedBy="department")
16+
private List<DeptEmployee> employees;
17+
18+
public Department(String name) {
19+
this.name = name;
20+
}
21+
22+
public long getId() {
23+
return id;
24+
}
25+
26+
public void setId(long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public List<DeptEmployee> getEmployees() {
39+
return employees;
40+
}
41+
42+
public void setEmployees(List<DeptEmployee> employees) {
43+
this.employees = employees;
44+
}
45+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.hibernate.entities;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.ManyToOne;
8+
9+
@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindByEmployeeNumber", query = "from DeptEmployee where employeeNumber = :employeeNo"),
10+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDesgination", query = "from DeptEmployee where employeeNumber = :employeeNumber"),
11+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_UpdateEmployeeDepartment", query = "Update DeptEmployee set department = :newDepartment where employeeNumber = :employeeNo"),
12+
@org.hibernate.annotations.NamedQuery(name = "DeptEmployee_FindAllByDepartment", query = "from DeptEmployee where department = :department", timeout = 1, fetchSize = 10) })
13+
@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_FindByEmployeeName", query = "select * from deptemployee emp where name=:name", resultClass = DeptEmployee.class),
14+
@org.hibernate.annotations.NamedNativeQuery(name = "DeptEmployee_UpdateEmployeeDesignation", query = "call UPDATE_EMPLOYEE_DESIGNATION(:employeeNumber, :newDesignation)", resultClass = DeptEmployee.class) })
15+
@Entity
16+
public class DeptEmployee {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
19+
private long id;
20+
21+
private String employeeNumber;
22+
23+
private String title;
24+
25+
private String name;
26+
27+
@ManyToOne
28+
private Department department;
29+
30+
public DeptEmployee(String name, String employeeNumber, Department department) {
31+
this.name = name;
32+
this.employeeNumber = employeeNumber;
33+
this.department = department;
34+
}
35+
36+
public DeptEmployee(String name, String employeeNumber, String title, Department department) {
37+
super();
38+
this.name = name;
39+
this.employeeNumber = employeeNumber;
40+
this.title = title;
41+
this.department = department;
42+
}
43+
44+
public long getId() {
45+
return id;
46+
}
47+
48+
public void setId(long id) {
49+
this.id = id;
50+
}
51+
52+
public String getEmployeeNumber() {
53+
return employeeNumber;
54+
}
55+
56+
public void setEmployeeNumber(String employeeNumber) {
57+
this.employeeNumber = employeeNumber;
58+
}
59+
60+
public String getName() {
61+
return name;
62+
}
63+
64+
public void setName(String name) {
65+
this.name = name;
66+
}
67+
68+
public Department getDepartment() {
69+
return department;
70+
}
71+
72+
public void setDepartment(Department department) {
73+
this.department = department;
74+
}
75+
76+
public String getTitle() {
77+
return title;
78+
}
79+
80+
public void setTitle(String title) {
81+
this.title = title;
82+
}
83+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public FindAll(Session session) {
2020
this.session = session;
2121
}
2222

23-
public List<Student> findAllWithJpql() {
23+
public List<Student> findAllStudentsWithJpql() {
2424
return session.createQuery("SELECT a FROM Student a", Student.class).getResultList();
2525
}
2626

27-
public List<Student> findAllWithCriteriaQuery() {
27+
public List<Student> findAllStudentsWithCriteriaQuery() {
2828
CriteriaBuilder cb = session.getCriteriaBuilder();
2929
CriteriaQuery<Student> cq = cb.createQuery(Student.class);
3030
Root<Student> rootEntry = cq.from(Student.class);
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)