|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.stateless.insert; |
6 | 6 |
|
7 | | -import org.hibernate.cfg.MappingSettings; |
8 | | - |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.JoinColumn; |
| 10 | +import jakarta.persistence.ManyToOne; |
| 11 | +import jakarta.persistence.Table; |
| 12 | +import org.hibernate.StatelessSession; |
9 | 13 | import org.hibernate.testing.orm.junit.DomainModel; |
10 | | -import org.hibernate.testing.orm.junit.ServiceRegistry; |
11 | 14 | import org.hibernate.testing.orm.junit.SessionFactory; |
12 | 15 | import org.hibernate.testing.orm.junit.SessionFactoryScope; |
13 | | -import org.hibernate.testing.orm.junit.Setting; |
14 | 16 | import org.junit.jupiter.api.AfterEach; |
15 | 17 | import org.junit.jupiter.api.Test; |
16 | 18 |
|
17 | 19 | /** |
18 | | - |
| 20 | + * Simple "smoke" test of {@linkplain StatelessSession#insert} with associations |
19 | 21 | */ |
20 | | -@ServiceRegistry(settings = @Setting(name = MappingSettings.TRANSFORM_HBM_XML, value = "true")) |
21 | | -@DomainModel(xmlMappings = "org/hibernate/orm/test/stateless/insert/Mappings.hbm.xml") |
| 22 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 23 | +@DomainModel(annotatedClasses = { |
| 24 | + StatelessSessionInsertTest.Department.class, |
| 25 | + StatelessSessionInsertTest.Employee.class, |
| 26 | +}) |
22 | 27 | @SessionFactory |
23 | 28 | public class StatelessSessionInsertTest { |
24 | 29 |
|
25 | 30 | @Test |
26 | 31 | public void testInsertWithForeignKey(SessionFactoryScope scope) { |
27 | | - Message msg = new Message(); |
28 | | - scope.inTransaction( |
29 | | - session -> { |
30 | | - final String messageId = "message_id"; |
31 | | - msg.setId( messageId ); |
32 | | - msg.setContent( "message_content" ); |
33 | | - msg.setSubject( "message_subject" ); |
34 | | - session.persist( msg ); |
35 | | - } |
36 | | - ); |
37 | | - |
38 | | - scope.inStatelessTransaction( |
39 | | - statelessSession -> { |
40 | | - MessageRecipient signature = new MessageRecipient(); |
41 | | - signature.setId( "recipient" ); |
42 | | - signature. setEmail( "[email protected]" ); |
43 | | - signature.setMessage( msg ); |
44 | | - statelessSession.insert( signature ); |
45 | | - } |
46 | | - ); |
| 32 | + final Department department = scope.fromTransaction( (session) -> { |
| 33 | + final Department dept = new Department( 1, "Marketing" ); |
| 34 | + session.persist( dept ); |
| 35 | + return dept; |
| 36 | + } ); |
| 37 | + |
| 38 | + scope.inStatelessTransaction( (statelessSession) -> { |
| 39 | + final Employee employee = new Employee( 1, "John Jacobs", department ); |
| 40 | + statelessSession.insert( employee ); |
| 41 | + } ); |
47 | 42 | } |
48 | 43 |
|
49 | 44 | @AfterEach |
50 | 45 | public void cleanup(SessionFactoryScope scope) { |
51 | | - scope.inTransaction( |
52 | | - session -> { |
53 | | - session.createQuery( "delete MessageRecipient" ).executeUpdate(); |
54 | | - session.createQuery( "delete Message" ).executeUpdate(); |
55 | | - } |
56 | | - ); |
| 46 | + scope.dropData(); |
| 47 | + } |
| 48 | + |
| 49 | + @Entity |
| 50 | + @Table(name = "departments") |
| 51 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 52 | + public static class Department { |
| 53 | + @Id |
| 54 | + private Integer id; |
| 55 | + private String name; |
| 56 | + |
| 57 | + public Department() { |
| 58 | + } |
| 59 | + |
| 60 | + public Department(Integer id, String name) { |
| 61 | + this.id = id; |
| 62 | + this.name = name; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Entity |
| 67 | + @Table(name = "employees") |
| 68 | + @SuppressWarnings({"FieldCanBeLocal", "unused"}) |
| 69 | + public static class Employee { |
| 70 | + @Id |
| 71 | + private Integer id; |
| 72 | + private String name; |
| 73 | + @ManyToOne |
| 74 | + @JoinColumn(name = "msg_fk") |
| 75 | + private Department department; |
| 76 | + |
| 77 | + public Employee() { |
| 78 | + } |
| 79 | + |
| 80 | + public Employee(Integer id, String name, Department department) { |
| 81 | + this.id = id; |
| 82 | + this.name = name; |
| 83 | + this.department = department; |
| 84 | + } |
57 | 85 | } |
58 | 86 | } |
0 commit comments