-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
hibernate-core/src/test/java/org/hibernate/orm/test/customsql/CustomSqlGeneratedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
69 changes: 69 additions & 0 deletions
69
hibernate-core/src/test/java/org/hibernate/orm/test/customsql/CustomSqlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|