This repository was archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
default to records ? #26
Comments
To rewrite the previous example using Java records and Hibernate without JPA, you can follow these steps:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-groovy-templates</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
</dependencies>
<!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">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.example.manualconfig.Person"/>
</session-factory>
</hibernate-configuration>
package com.example.manualconfig;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml")
.build();
Metadata metadata = new MetadataSources(standardRegistry)
.getMetadataBuilder()
.build();
return metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
throw new ExceptionInInitializerError("Error building SessionFactory: " + e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
package com.example.manualconfig;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public record Person(
@Id
@GeneratedValue(generator = "tsid-generator")
@GenericGenerator(name = "tsid-generator", strategy = "com.example.manualconfig.TsidGenerator")
String id,
String name) {
}
@RestController
public class MyController {
private final MyService myService;
private static final Logger logger = LoggerFactory.getLogger(MyController.class);
@Autowired
public MyController(MyService myService) {
this.myService = myService;
}
@GetMapping("/")
public String hello() {
return myService.getMessage();
}
@GetMapping("/log")
public String log() {
logger.trace("This is a TRACE level log message");
logger.debug("This is a DEBUG level log message");
logger.info("This is an INFO level log message");
logger.warn("This is a WARN level log message");
logger.error("This is an ERROR level log message");
return "Logged messages with different severity levels";
}
@PostMapping("/person")
public Person createPerson(@RequestParam String name) {
Person person = new Person(null, name);
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
Transaction transaction = session.beginTransaction();
session.save(person);
transaction.commit();
}
return person;
}
@GetMapping("/person/{id}")
public Person getPerson(@PathVariable String id) {
Person person;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
person = session.get(Person.class, id);
}
return person;
}
} Now, when you run the |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
https://twitter.com/maciejwalkowiak/status/1458618723024187393
https://twitter.com/piotr_minkowski/status/1419644115696955396
https://www.sivalabs.in/using-java-records-with-spring-boot-3/
The text was updated successfully, but these errors were encountered: