tests for HHH-15663 and HHH-15570

This commit is contained in:
Gavin King 2022-11-04 11:22:11 +01:00
parent 383ffa56eb
commit f6e65dc91a
3 changed files with 139 additions and 2 deletions

View File

@ -16,7 +16,6 @@ import jakarta.persistence.Id;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.ValueGenerationType;
import org.hibernate.dialect.SybaseASEDialect;
import org.hibernate.tuple.AnnotationValueGeneration;
import org.hibernate.tuple.GenerationTiming;
import org.hibernate.tuple.ValueGenerator;
@ -26,7 +25,6 @@ import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

View File

@ -0,0 +1,70 @@
package org.hibernate.orm.test.customsql;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hibernate.annotations.GenerationTime.ALWAYS;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SessionFactory
@DomainModel(annotatedClasses = CustomSqlGeneratedTest.Custom.class)
public class CustomSqlGeneratedTest {
@Test
public void testCustomSqlWithGenerated(SessionFactoryScope scope) {
Custom c = new Custom();
c.name = "name";
c.text = "text";
scope.inTransaction(s->{
s.persist(c);
s.flush();
Custom cc = s.find(Custom.class, c.id);
assertEquals(cc.text, "TEXT");
assertEquals(cc.name, "NAME");
cc.name = "eman";
cc.text = "more text";
s.flush();
cc = s.find(Custom.class, c.id);
assertEquals(cc.text, "MORE TEXT");
assertEquals(cc.name, "EMAN");
s.remove(cc);
s.flush();
cc = s.find(Custom.class, c.id);
assertEquals(cc.text, "DELETED");
assertEquals(cc.name, "DELETED");
});
}
@Entity
@Table(name = "CustomPrimary")
@SecondaryTable(name = "CustomSecondary")
@SQLInsert(sql="insert into CustomPrimary (name, revision, id) values (upper(?),?,?)")
@SQLInsert(table = "CustomSecondary", sql="insert into CustomSecondary (text, id) values (upper(?),?)")
@SQLUpdate(sql="update CustomPrimary set name = upper(?), revision = ? where id = ? and revision = ?")
@SQLUpdate(table = "CustomSecondary", sql="update CustomSecondary set text = upper(?) where id = ?")
@SQLDelete(sql="update CustomPrimary set name = 'DELETED' where id = ? and revision = ?")
@SQLDelete(table = "CustomSecondary", sql="update CustomSecondary set text = 'DELETED' where id = ?")
static class Custom {
@Id @GeneratedValue
Long id;
@Version @Column(name = "revision")
int version;
@Generated(value = ALWAYS, writable = true)
String name;
@Generated(value = ALWAYS, writable = true)
@Column(table = "CustomSecondary")
String text;
}
}

View File

@ -0,0 +1,69 @@
package org.hibernate.orm.test.customsql;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SessionFactory
@DomainModel(annotatedClasses = CustomSqlTest.Custom.class)
public class CustomSqlTest {
@Test
public void testCustomSql(SessionFactoryScope scope) {
Custom c = new Custom();
c.name = "name";
c.text = "text";
scope.inTransaction(s->{
s.persist(c);
s.flush();
s.clear();
Custom cc = s.find(Custom.class, c.id);
assertEquals(cc.text, "TEXT");
assertEquals(cc.name, "NAME");
cc.name = "eman";
cc.text = "more text";
s.flush();
s.clear();
cc = s.find(Custom.class, c.id);
assertEquals(cc.text, "MORE TEXT");
assertEquals(cc.name, "EMAN");
s.remove(cc);
s.flush();
s.clear();
cc = s.find(Custom.class, c.id);
assertEquals(cc.text, "DELETED");
assertEquals(cc.name, "DELETED");
});
}
@Entity
@Table(name = "CustomPrimary")
@SecondaryTable(name = "CustomSecondary")
@SQLInsert(sql="insert into CustomPrimary (name, revision, id) values (upper(?),?,?)")
@SQLInsert(table = "CustomSecondary", sql="insert into CustomSecondary (text, id) values (upper(?),?)")
@SQLUpdate(sql="update CustomPrimary set name = upper(?), revision = ? where id = ? and revision = ?")
@SQLUpdate(table = "CustomSecondary", sql="update CustomSecondary set text = upper(?) where id = ?")
@SQLDelete(sql="update CustomPrimary set name = 'DELETED' where id = ? and revision = ?")
@SQLDelete(table = "CustomSecondary", sql="update CustomSecondary set text = 'DELETED' where id = ?")
static class Custom {
@Id @GeneratedValue
Long id;
@Version @Column(name = "revision")
int version;
String name;
@Column(table = "CustomSecondary")
String text;
}
}