This commit is contained in:
Struchkov Mark 2022-07-09 17:28:52 +03:00
commit c9d933ecb3
7 changed files with 303 additions and 0 deletions

39
.gitignore vendored Normal file
View File

@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
/.idea/

BIN
data/demo.mv.db Normal file

Binary file not shown.

46
pom.xml Normal file
View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.struchkov.example</groupId>
<artifactId>hibernates-statelesssession</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.1.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,137 @@
package dev.struchkov.example.hibernate.statelesssession;
import dev.struchkov.example.hibernate.statelesssession.domain.Post;
import dev.struchkov.example.hibernate.statelesssession.domain.PostComment;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
final SessionFactory sessionFactory = getSessionFactory();
firstExample(sessionFactory);
// secondExample(sessionFactory);
// thirdExample(sessionFactory);
// thirdExampleWithSession(sessionFactory);
}
private static void firstExample(SessionFactory sessionFactory) {
final StatelessSession statelessSession = sessionFactory.openStatelessSession();
statelessSession.getTransaction().begin();
final Post post = new Post();
post.setTitle("New Past");
statelessSession.insert(post);
post.setTitle("New Post");
statelessSession.update(post);
statelessSession.getTransaction().commit();
}
private static void secondExample(SessionFactory sessionFactory) {
final StatelessSession statelessSession = sessionFactory.openStatelessSession();
statelessSession.getTransaction().begin();
final Post post = statelessSession.createQuery("""
SELECT p
FROM Post p
JOIN FETCH p.comments
WHERE p.id=:id""", Post.class)
.setParameter("id", 1L)
.getSingleResult();
log.info(post.getId() + " " + post.getTitle());
log.info("Comments size: " + post.getComments().size());
statelessSession.getTransaction().commit();
}
private static void thirdExample(SessionFactory sessionFactory) {
final StatelessSession statelessSession = sessionFactory.openStatelessSession();
statelessSession.getTransaction().begin();
final Post post1 = statelessSession.createQuery("""
SELECT p
FROM Post p
JOIN FETCH p.comments
WHERE p.id=:id""", Post.class)
.setParameter("id", 1L)
.getSingleResult();
final Post post2 = statelessSession.createQuery("""
SELECT p
FROM Post p
JOIN FETCH p.comments
WHERE p.id=:id""", Post.class)
.setParameter("id", 1L)
.getSingleResult();
System.out.println();
statelessSession.getTransaction().commit();
}
private static void thirdExampleWithSession(SessionFactory sessionFactory) {
final Session session = sessionFactory.openSession();
session.getTransaction().begin();
final Post post1 = session.createQuery("""
SELECT p
FROM Post p
JOIN FETCH p.comments
WHERE p.id=:id""", Post.class)
.setParameter("id", 1L)
.getSingleResult();
final Post post2 = session.createQuery("""
SELECT p
FROM Post p
JOIN FETCH p.comments
WHERE p.id=:id""", Post.class)
.setParameter("id", 1L)
.getSingleResult();
System.out.println();
session.getTransaction().commit();
session.close();
}
public static SessionFactory getSessionFactory() {
final Properties settings = new Properties();
settings.put(AvailableSettings.DRIVER, "org.h2.Driver");
settings.put(AvailableSettings.URL, "jdbc:h2:file:./data/demo");
settings.put(AvailableSettings.USER, "sa");
settings.put(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect");
settings.put(AvailableSettings.FORMAT_SQL, "true");
settings.put(AvailableSettings.HBM2DDL_AUTO, "none");
final Configuration configuration = new Configuration();
configuration.setProperties(settings);
configuration.addAnnotatedClass(Post.class);
configuration.addAnnotatedClass(PostComment.class);
final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
}

View File

@ -0,0 +1,41 @@
package dev.struchkov.example.hibernate.statelesssession.domain;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
public class Post {
@Id
@GeneratedValue
private Long id;
private String title;
@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<PostComment> comments = new ArrayList<>();
public void addComment(PostComment postComment) {
postComment.setPost(this);
comments.add(postComment);
}
}

View File

@ -0,0 +1,25 @@
package dev.struchkov.example.hibernate.statelesssession.domain;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter
@Setter
public class PostComment {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Post post;
private String review;
}

View File

@ -0,0 +1,15 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate.SQL" level="DEBUG" />
<logger name="org.hibernate.type" level="TRACE" />
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>