BAEL-5549: feat: Hibernate Keywords - using keywords as table column names (#13355)
* BAEL-5549: feat: Hibernate Keywords - using keywords as table column names * BAEL-5549: feat: Hibernate Keywords - using keywords as table column names * BAEL-5549: feat: Hibernate Keywords - using keywords as table column names * BAEL-5549: fix: Hibernate Keywords - alter the "where" value to make sure it is obvious that it is not the value but the @Column that is causing the error. * BAEL-5549: doc: Hibernate Keywords - revert README changes
This commit is contained in:
parent
68522540e5
commit
a3f2e96feb
@ -79,6 +79,12 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>mysql</artifactId>
|
||||
<version>${testcontainers.mysql.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
@ -88,6 +94,7 @@
|
||||
<mysql.version>6.0.6</mysql.version>
|
||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||
<h2.version>2.1.214</h2.version>
|
||||
<testcontainers.mysql.version>1.17.6</testcontainers.mysql.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,39 @@
|
||||
package com.baeldung.hibernate.keywords;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "broken_phone_order")
|
||||
public class BrokenPhoneOrder implements Serializable {
|
||||
@Id
|
||||
@Column(name = "order")
|
||||
String order;
|
||||
@Column(name = "where")
|
||||
String where;
|
||||
|
||||
public BrokenPhoneOrder(String order, String where) {
|
||||
this.order = order;
|
||||
this.where = where;
|
||||
}
|
||||
|
||||
public String getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(String order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public String getWhere() {
|
||||
return where;
|
||||
}
|
||||
|
||||
public void setWhere(String where) {
|
||||
this.where = where;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.baeldung.hibernate.keywords;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "phone_order")
|
||||
public class PhoneOrder implements Serializable {
|
||||
@Id
|
||||
@Column(name = "`order`")
|
||||
String order;
|
||||
@Column(name = "`where`")
|
||||
String where;
|
||||
|
||||
public PhoneOrder(String order, String where) {
|
||||
this.order = order;
|
||||
this.where = where;
|
||||
}
|
||||
|
||||
public String getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(String order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public String getWhere() {
|
||||
return where;
|
||||
}
|
||||
|
||||
public void setWhere(String where) {
|
||||
this.where = where;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.baeldung.hibernate.keywords;
|
||||
|
||||
import static java.util.UUID.randomUUID;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HibernateKeywordsApplicationIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
private Session session;
|
||||
|
||||
@BeforeAll
|
||||
static void createSession() {
|
||||
sessionFactory = new Configuration().addAnnotatedClass(BrokenPhoneOrder.class)
|
||||
.addAnnotatedClass(PhoneOrder.class)
|
||||
.configure("keywords/hibernate.keywords.cfg.xml")
|
||||
.buildSessionFactory();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void before() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void after() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBrokenPhoneOrderWithReservedKeywords_whenNewObjectIsPersisted_thenItFails() {
|
||||
BrokenPhoneOrder order = new BrokenPhoneOrder(randomUUID().toString(), "My House");
|
||||
|
||||
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> {
|
||||
session.persist(order);
|
||||
session.flush();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPhoneOrderWithEscapedKeywords_whenNewObjectIsPersisted_thenItSucceeds() {
|
||||
PhoneOrder order = new PhoneOrder(randomUUID().toString(), "here");
|
||||
|
||||
session.persist(order);
|
||||
session.flush();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?xml version = "1.0" encoding = "utf-8"?>
|
||||
<!DOCTYPE hibernate-configuration SYSTEM
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
|
||||
<session-factory>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||
<property name="connection.url">jdbc:tc:mysql:5.7.41:///restaurant?TC_INITSCRIPT=keywords/init.sql</property>
|
||||
<property name="hibernate.connection.driver_class">org.testcontainers.jdbc.ContainerDatabaseDriver</property>
|
||||
<property name="hibernate.hbm2ddl.auto">validate</property>
|
||||
<property name="show_sql">true</property>
|
||||
</session-factory>
|
||||
|
||||
</hibernate-configuration>
|
@ -0,0 +1,9 @@
|
||||
CREATE TABLE IF NOT EXISTS phone_order (
|
||||
`order` varchar(255) PRIMARY KEY,
|
||||
`where` varchar(255)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS broken_phone_order (
|
||||
`order` varchar(255) PRIMARY KEY,
|
||||
`where` varchar(255)
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user